0

When I do number formatting using Javascript, after 15 digits it's rounding off the number. As we are working on currency, don't want this rounding to happen. We are using angular as our client-side lib, tried using angular format number and format currency as well, but didn't help. Can anyone suggest any way to fix this issue?

We need the number to be formatted as per the current locale(like if the user is inputting 10000, we need to format it as per the locale set to 10,000) but don't want this rounding issue to happen for big numbers(those with a value above - 9999999999999999).

const number = 9999999999999999;
console.log(number.toLocaleString('en-US'));
Vishnu
  • 897
  • 6
  • 13
  • 1
    Duplicate: [Large numbers erroneously rounded in JavaScript](https://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript) (btw, angular is completely irrelevant here) –  Dec 06 '21 at 18:26
  • 1
    The `number` type isn't well-suited to currency, because although fast, it's imprecise. (Famously, `0.1 + 0.2` is `0.30000000000000004`.) For currency work, you might consider `BigInt` making 1 the smallest fraction of the currency you want to track. (For instance, in the U.S., 1 might be a penny or, for some applications, a 10th or 100th; in India, 1 might be a 100 paise or maybe 1 rupee.) `BigInt` doesn't have the problem with being imprecise at very high whole values like `number` does. – T.J. Crowder Dec 06 '21 at 18:31
  • For instance, `const number = 9999999999999999n; console.log(number.toLocaleString("en-US"));` is `"9,999,999,999,999,999"`. (The `n` suffix means `BigInt`.) – T.J. Crowder Dec 06 '21 at 18:32

0 Answers0