Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [cherry-pick][V12.1.0] fix loss of precision for very big values #26496

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ const PermitSimulation: React.FC<{
const { tokenValue, tokenValueMaxPrecision } = useMemo(() => {
const tokenAmount = calcTokenAmount(value, tokenDecimals);

// FIXME - Precision may be lost for large values when using formatAmount
/** @see {@link https://github.com/MetaMask/metamask-extension/issues/25755} */
return {
tokenValue: formatAmount('en-US', tokenAmount),
tokenValueMaxPrecision: formatAmountMaxPrecision('en-US', tokenAmount),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ describe('formatAmount', () => {
[47361034.006, '47,361,034'],
['12130982923409.5', '12,130,982,923,410'],
['1213098292340944.5', '1,213,098,292,340,945'],
// Precision is lost after the value is greator than Number.MAX_SAFE_INTEGER. The digits after
// the 15th digit become 0's.
// TODO fix the precision
/** @see {@link https://github.com/MetaMask/metamask-extension/issues/25755} */
['30001231231212312138768', '30,001,231,231,212,312,000,000'],
['30001231231212312138768', '30,001,231,231,212,312,138,768'],
[
'115792089237316195423570985008687907853269984665640564039457584007913129639935',
'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',
'1157920892373161954235709850086879078532699846656405640394575.84007913129639935',
'1,157,920,892,373,161,954,235,709,850,086,879,078,532,699,846,656,405,640,394,576',
],
])(
'formats amount greater than or equal to 1 with appropriate decimal precision (%s => %s)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ export function formatAmountMaxPrecision(
locale: string,
num: number | BigNumber,
): string {
return new Intl.NumberFormat(locale, {
minimumSignificantDigits: 1,
}).format(new BigNumber(num.toString()).toNumber());
const bigNumberValue = new BigNumber(num);
const numberOfDecimals = bigNumberValue.decimalPlaces();
const formattedValue = bigNumberValue.toFixed(numberOfDecimals);

const [integerPart, fractionalPart] = formattedValue.split('.');
const formattedIntegerPart = new Intl.NumberFormat(locale).format(
integerPart as unknown as number,
);

return fractionalPart
? `${formattedIntegerPart}.${fractionalPart}`
: formattedIntegerPart;
}

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

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