Skip to content

Commit e106cd3

Browse files
committed
Change in number format to fix loss of precision for very big values
1 parent f9f3cff commit e106cd3

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

ui/pages/confirmations/components/simulation-details/formatAmount.test.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@ describe('formatAmount', () => {
3737
[47361034.006, '47,361,034'],
3838
['12130982923409.5', '12,130,982,923,410'],
3939
['1213098292340944.5', '1,213,098,292,340,945'],
40-
// Precision is lost after the value is greator than Number.MAX_SAFE_INTEGER. The digits after
41-
// the 15th digit become 0's.
42-
// TODO fix the precision
43-
/** @see {@link https://github.com/MetaMask/metamask-extension/issues/25755} */
44-
['30001231231212312138768', '30,001,231,231,212,312,000,000'],
40+
['30001231231212312138768', '30,001,231,231,212,312,138,768'],
4541
[
46-
'115792089237316195423570985008687907853269984665640564039457584007913129639935',
47-
'115,792,089,237,316,200,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000',
42+
'1157920892373161954235709850086879078532699846656405640394575.84007913129639935',
43+
'1,157,920,892,373,161,954,235,709,850,086,879,078,532,699,846,656,405,640,394,576',
4844
],
4945
])(
5046
'formats amount greater than or equal to 1 with appropriate decimal precision (%s => %s)',

ui/pages/confirmations/components/simulation-details/formatAmount.ts

+6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ export function formatAmount(locale: string, amount: BigNumber): string {
8080
MAX_SIGNIFICANT_DECIMAL_PLACES - digitsLeftOfDecimal + 1,
8181
);
8282

83+
if (maximumFractionDigits === 0) {
84+
// No decimals to display – we can use BigInt to localize without loss of precision.
85+
return BigInt(amount.toFixed(0)).toLocaleString(locale);
86+
}
87+
88+
// At most 4 (MAX_SIGNIFICANT_DECIMAL_PLACES + 1) significant digits – Use Intl.NumberFormat to localize
8389
return new Intl.NumberFormat(locale, {
8490
maximumFractionDigits,
8591
} as Intl.NumberFormatOptions).format(amount.toNumber());

0 commit comments

Comments
 (0)