Saving high precision decimal numbers to SQlite

This commit is contained in:
Stedd 2022-02-13 15:13:23 +01:00
parent 6d8bf44077
commit 9ef6358089
3 changed files with 48 additions and 26 deletions

View File

@ -72,10 +72,12 @@ namespace CryptoCalc
public long WalletID { get; set; }
public string Currency { get; set; }
public string Amount { get; set; }
public long Amount { get; set; }
public ulong AmountDecimal { get; set; }
public string TransactionType { get; set; }
public string FeeCurrency { get; set; }
public string FeeAmount { get; set; }
public long FeeAmount { get; set; }
public ulong FeeAmountDecimal { get; set; }
public string Platform { get; set; }
public string Note { get; set; }
@ -85,12 +87,12 @@ namespace CryptoCalc
public Transaction() { SetDBStrings(); }
public Transaction(long walletID, string currency, string amount, string type)
public Transaction(long walletID, string currency, decimal amount, string type)
{
SaveUnixTimeNow();
WalletID = walletID;
Currency = currency;
Amount = amount;
(Amount, AmountDecimal) = Util.SplitDecimal(amount);
TransactionType = type;
Platform = "";
@ -99,14 +101,14 @@ namespace CryptoCalc
SetDBStrings();
}
public Transaction(string currency, string amount, string type, string feeCurrency, string feeAmount)
public Transaction(string currency, decimal amount, string type, string feeCurrency, decimal feeAmount)
{
SaveUnixTimeNow();
Currency = currency;
Amount = amount.ToString(CultureInfo.InvariantCulture);
(Amount, AmountDecimal) = Util.SplitDecimal(amount);
TransactionType = type;
FeeCurrency = feeCurrency;
FeeAmount = feeAmount.ToString(CultureInfo.InvariantCulture);
(FeeAmount, FeeAmountDecimal) = Util.SplitDecimal(feeAmount);
Platform = "";
Note = "";
@ -124,14 +126,16 @@ namespace CryptoCalc
+ $"@{nameof(UnixTime)},"
+ $"@{nameof(Currency)},"
+ $"@{nameof(Amount)},"
+ $"@{nameof(AmountDecimal)},"
+ $"@{nameof(TransactionType)},"
+ $"@{nameof(FeeCurrency)},"
+ $"@{nameof(FeeAmount)},"
+ $"@{nameof(FeeAmountDecimal)},"
+ $"@{nameof(Platform)},"
+ $"@{nameof(Note)}";
}
public string FullInfo => $"{ Index } {WalletID} { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { decimal.Parse(Amount, CultureInfo.InvariantCulture) } { TransactionType }";
public string FullInfo => $"{ Index } {WalletID} { GetLocalTimeFromUnixTime(UnixTime) } { Currency } { Util.CombineDecimal(Amount, AmountDecimal) } { TransactionType }";
public string CreateTable(string walletTableName)
{
@ -141,10 +145,12 @@ namespace CryptoCalc
+ $"\"{nameof(WalletID)}\" INTEGER NOT NULL,"
+ $"\"{nameof(UnixTime)}\" INTEGER NOT NULL,"
+ $"\"{nameof(Currency)}\" TEXT NOT NULL,"
+ $"\"{nameof(Amount)}\" TEXT NOT NULL,"
+ $"\"{nameof(Amount)}\" INTEGER NOT NULL,"
+ $"\"{nameof(AmountDecimal)}\" INTEGER NOT NULL,"
+ $"\"{nameof(TransactionType)}\" TEXT NOT NULL,"
+ $"\"{nameof(FeeCurrency)}\" TEXT,"
+ $"\"{nameof(FeeAmount)}\" TEXT,"
+ $"\"{nameof(FeeAmount)}\" INTEGER,"
+ $"\"{nameof(FeeAmountDecimal)}\" INTEGER,"
+ $"\"{nameof(Platform)}\" TEXT,"
+ $"\"{nameof(Note)}\" TEXT,"
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT),"
@ -166,7 +172,8 @@ namespace CryptoCalc
public string Platform { get; set; }
public string Name { get; set; }
public string Currency { get; set; }
public ulong Balance { get; set; }
public long Balance { get; set; }
public ulong BalanceDecimal { get; set; }
public int DefaultWallet { get; set; }
public string Note { get; set; }
@ -183,7 +190,7 @@ namespace CryptoCalc
Platform = platform;
Name = name;
Currency = currency;
Balance = Util.ConvToLong(0M);
(Balance, BalanceDecimal) = Util.SplitDecimal(0);
DefaultWallet = 0;
Note = note;
@ -203,6 +210,7 @@ namespace CryptoCalc
+ $"@{nameof(Name)},"
+ $"@{nameof(Currency)},"
+ $"@{nameof(Balance)},"
+ $"@{nameof(BalanceDecimal)},"
+ $"@{nameof(DefaultWallet)},"
+ $"@{nameof(Note)}";
}
@ -219,7 +227,8 @@ namespace CryptoCalc
+ $"\"{nameof(Platform)}\" TEXT,"
+ $"\"{nameof(Name)}\" TEXT,"
+ $"\"{nameof(Currency)}\" TEXT NOT NULL,"
+ $"\"{nameof(Balance)}\" TEXT NOT NULL,"
+ $"\"{nameof(Balance)}\" INTEGER NOT NULL,"
+ $"\"{nameof(BalanceDecimal)}\" INTEGER NOT NULL,"
+ $"\"{nameof(DefaultWallet)}\" INTEGER,"
+ $"\"{nameof(Note)}\" TEXT,"
+ $"PRIMARY KEY(\"{nameof(Index)}\" AUTOINCREMENT)"

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
@ -49,7 +50,7 @@ namespace CryptoCalc
new Transaction(
selectedWallet.Index,
inputCurrency.Text,
inputAmount.Text,
Convert.ToDecimal(inputAmount.Text, CultureInfo.InvariantCulture),
inputType.Text
)
);
@ -110,7 +111,7 @@ namespace CryptoCalc
t.WalletID = selectedWallet.Index;
t.SaveUnixTimeNow();
t.Currency = "SOL";
t.Amount = (30m * (decimal)rand.NextDouble()).ToString();
(t.Amount, t.AmountDecimal) = Util.SplitDecimal(Convert.ToDecimal(30m * (decimal)rand.NextDouble()));
t.TransactionType = "BUY";
t.Platform = "Firi";
t.Note = "Test";
@ -125,7 +126,7 @@ namespace CryptoCalc
w.Platform = "Ledger";
w.Name = "DefaultBTCWallet";
w.Currency = "BTC";
w.Balance = Util.ConvToLong(0);//Convert.ToSingle(30 * rand.NextDouble());
w.Balance = 0;//Convert.ToSingle(30 * rand.NextDouble());
w.DefaultWallet = 1;
w.Note = "Main BTC Wallet";
return w;
@ -134,8 +135,8 @@ namespace CryptoCalc
private void inputAmount_LostFocus(object sender, RoutedEventArgs e)
{
inputAmount.Text = inputAmount.Text.Replace(".", ",");
//inputAmount.Text = inputAmount.Text.Replace(",", ".");
//inputAmount.Text = inputAmount.Text.Replace(".", ",");
inputAmount.Text = inputAmount.Text.Replace(",", ".");
}
private void currencyText_LostFocus(object sender, RoutedEventArgs e)

22
Util.cs
View File

@ -4,21 +4,33 @@ namespace CryptoCalc
{
public class Util
{
private static readonly decimal lowestUnit = 1e18m;
public static string GetConnectionString(string name = "Default")
{
return ConfigurationManager.ConnectionStrings[name].ConnectionString;
}
public static ulong ConvToLong(decimal value)
public static ulong ConvToULong(decimal value)
{
//return decimal.ToInt64(value * 1e18M);
return decimal.ToUInt64(value * 1e18M);
return decimal.ToUInt64(value * lowestUnit);
}
public static decimal ConveToDecimal (ulong value)
public static decimal ConvToDecimal(ulong value)
{
return value * 1e-18M;
return value / lowestUnit;
}
public static (long wholeNumber, ulong fractionNumber) SplitDecimal(decimal value)
{
long w = (long)value;
ulong f = ConvToULong((value % 1));
return (w, f);
}
public static decimal CombineDecimal(long amount, ulong amountDecimal)
{
return amount + ConvToDecimal(amountDecimal);
}
}
}