indicate loss of precision in displayed amounts

This commit is contained in:
julian 2023-05-29 17:13:45 -06:00
parent 8155e4afd4
commit 4b6afe1db0
2 changed files with 29 additions and 4 deletions

View file

@ -134,6 +134,9 @@ extension AmountUnitExt on AmountUnit {
// start building the return value with just the whole value
String returnValue = wholeNumber.toString();
// if true and withUnitName is true, we will show "~" prepended on amount
bool didLosePrecision = false;
// if any decimal places should be shown continue building the return value
if (places > 0) {
// get the fractional value
@ -162,6 +165,13 @@ extension AmountUnitExt on AmountUnit {
}
if (remainder.length > actualDecimalPlaces) {
// check for loss of precision
final remainingRemainder =
BigInt.tryParse(remainder.substring(actualDecimalPlaces));
if (remainingRemainder != null) {
didLosePrecision = remainingRemainder > BigInt.zero;
}
// trim unwanted trailing digits
remainder = remainder.substring(0, actualDecimalPlaces);
} else if (remainder.length < actualDecimalPlaces) {
@ -190,6 +200,11 @@ extension AmountUnitExt on AmountUnit {
if (tokenContract != null) {
overrideUnit = unitForContract(tokenContract);
}
if (didLosePrecision) {
returnValue = "~$returnValue";
}
return "$returnValue ${overrideUnit ?? unitForCoin(coin)}";
}
}

View file

@ -68,7 +68,17 @@ void main() {
coin: Coin.ethereum,
maxDecimalPlaces: 8,
),
"10.12345678 ETH",
"~10.12345678 ETH",
);
expect(
AmountUnit.normal.displayAmount(
amount: amount,
locale: "en_US",
coin: Coin.ethereum,
maxDecimalPlaces: 4,
),
"~10.1234 ETH",
);
expect(
@ -88,7 +98,7 @@ void main() {
coin: Coin.ethereum,
maxDecimalPlaces: 9,
),
"10123.456789123 mETH",
"~10123.456789123 mETH",
);
expect(
@ -98,7 +108,7 @@ void main() {
coin: Coin.ethereum,
maxDecimalPlaces: 8,
),
"10123456.78912345 µETH",
"~10123456.78912345 µETH",
);
expect(
@ -108,7 +118,7 @@ void main() {
coin: Coin.ethereum,
maxDecimalPlaces: 1,
),
"10123456789.1 gwei",
"~10123456789.1 gwei",
);
expect(