0

In my application, I need to handle some currency fields which deals upto 21 number of digits. And the values needed to be calculated for further actions.

I have used parseFloat in the calculations previously when the maximum digit is < 15. Now since the requirement is 21 digits, the parseFloat is not supporting properly. It supports up to 17 digits properly. But after 17th digits, it rounds off the value.

Tried in Console window

So is there any alternatives to parseFloat? or any other methods through which I can handle this issue?

Application Background:

  • C# Application
  • AngularJS, Javascript involved
  • Database: SQL
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
The JD
  • 13
  • 12

1 Answers1

1

No, JS can only handle numbers of 16 integers, up to a maximum of 9,007,199,254,740,991 (aka Number.MAX_SAFE_INTEGER). It's a limitation of the language.

You can store them as strings if you simply need to display the values, but as soon as you perform a numerical operation on them JS will convert them to a Number type with the highest precision it can.

If you need a higher precision, then I would suggest you perform the calculation on the server and return the result as a string to your JS code.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339