Round to significant figures
MODIFY
WalletWasabi.Fluent/Helpers/MathUtils.cs
WalletWasabi.Fluent/Helpers/MathUtils.cs
namespace WalletWasabi.Fluent.Helpers;
-public class MathUtils
+public static class MathUtils
{
public static int Round(int n, int precision)
{
@@ -9,4 +9,22 @@ public class MathUtils
var rounded = roundedFraction * precision;
return (int)rounded;
}
+
+ public static decimal RoundToSignificantFigures(this decimal num, int n)
+ {
+ if (num == 0)
+ {
+ return 0;
+ }
+
+ int d = (int)Math.Ceiling(Math.Log10((double)Math.Abs(num)));
+ int power = n - d;
+
+ decimal magnitude = (decimal)Math.Pow(10, power);
+
+ decimal shifted = Math.Round(num * magnitude, 0, MidpointRounding.AwayFromZero);
+ decimal ret = shifted / magnitude;
+
+ return ret;
+ }
}

MODIFY
WalletWasabi.Fluent/ViewModels/Wallets/Send/ChangeAvoidanceSuggestionViewModel.cs
WalletWasabi.Fluent/ViewModels/Wallets/Send/ChangeAvoidanceSuggestionViewModel.cs
@@ -15,6 +15,7 @@ namespace WalletWasabi.Fluent.ViewModels.Wallets.Send;
public partial class ChangeAvoidanceSuggestionViewModel : SuggestionViewModel
{
+ private const int SignificantFiguresForFiatAmount = 3;
[AutoNotify] private string _amount;
[AutoNotify] private string _amountFiat;
[AutoNotify] private string? _differenceFiat;
@@ -29,15 +30,16 @@ public partial class ChangeAvoidanceSuggestionViewModel : SuggestionViewModel
var totalAmount = transactionResult.CalculateDestinationAmount();
var total = totalAmount.ToDecimal(MoneyUnit.BTC);
- _amountFiat = total.GenerateFiatText(fiatExchangeRate, "USD");
+ _amountFiat = total.RoundToSignificantFigures(SignificantFiguresForFiatAmount).GenerateFiatText(fiatExchangeRate, "USD");
var fiatTotal = total * fiatExchangeRate;
var fiatOriginal = originalAmount * fiatExchangeRate;
var fiatDifference = fiatTotal - fiatOriginal;
+ var roundedFiatDifference = fiatDifference.RoundToSignificantFigures(SignificantFiguresForFiatAmount);
_differenceFiat = (fiatDifference > 0
- ? $"{fiatDifference.GenerateFiatText("USD")} More"
- : $"{Math.Abs(fiatDifference).GenerateFiatText("USD")} Less")
+ ? $"{roundedFiatDifference.GenerateFiatText("USD")} More"
+ : $"{Math.Abs(roundedFiatDifference).GenerateFiatText("USD")} Less")
.Replace("(", "").Replace(")", "");
_amount = $"{totalAmount.ToFormattedString()} BTC";

Round to significant figures