Skip to content

Commit 30dce33

Browse files
authored
fix: change in number format to fix loss of precision for very big values (#25968)
1 parent 32dc79f commit 30dce33

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

ui/pages/confirmations/components/confirm/info/typed-sign/permit-simulation/permit-simulation.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ const PermitSimulation: React.FC<{
5555
const { tokenValue, tokenValueMaxPrecision } = useMemo(() => {
5656
const tokenAmount = calcTokenAmount(value, tokenDecimals);
5757

58-
// FIXME - Precision may be lost for large values when using formatAmount
59-
/** @see {@link https://github.com/MetaMask/metamask-extension/issues/25755} */
6058
return {
6159
tokenValue: formatAmount('en-US', tokenAmount),
6260
tokenValueMaxPrecision: formatAmountMaxPrecision('en-US', tokenAmount),

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ export function formatAmountMaxPrecision(
1313
locale: string,
1414
num: number | BigNumber,
1515
): string {
16-
return new Intl.NumberFormat(locale, {
17-
minimumSignificantDigits: 1,
18-
}).format(new BigNumber(num.toString()).toNumber());
16+
const bigNumberValue = new BigNumber(num);
17+
const numberOfDecimals = bigNumberValue.decimalPlaces();
18+
const formattedValue = bigNumberValue.toFixed(numberOfDecimals);
19+
20+
const [integerPart, fractionalPart] = formattedValue.split('.');
21+
const formattedIntegerPart = new Intl.NumberFormat(locale).format(
22+
integerPart as unknown as number,
23+
);
24+
25+
return fractionalPart
26+
? `${formattedIntegerPart}.${fractionalPart}`
27+
: formattedIntegerPart;
1928
}
2029

2130
/**
@@ -82,5 +91,10 @@ export function formatAmount(locale: string, amount: BigNumber): string {
8291

8392
return new Intl.NumberFormat(locale, {
8493
maximumFractionDigits,
85-
} as Intl.NumberFormatOptions).format(amount.toNumber());
94+
} as Intl.NumberFormatOptions).format(
95+
// string is valid parameter for format function
96+
// for some reason it gives TS issue
97+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/format#number
98+
amount.toFixed(maximumFractionDigits) as unknown as number,
99+
);
86100
}

0 commit comments

Comments
 (0)