Interested in the question of whether there are similar libraries in javascript, as in PHP (for example, BCMATH ) for performing arithmetic operations with the necessary accuracy?

Ps From 0.2 subtract 0.01808 and get 0.18192000000000003

  • I understand, everyone understands what is at stake, and here I am not). And what does "with the necessary accuracy" mean? What result do you need when weaning from 0.2 0.01808 ? - Dmytryk

1 answer 1

Use the toFixed method, I don’t know about the library, but you can write a function and specify an accuracy parameter.

 const result = 0.2 - 0.01808; console.log(+result.toFixed(5)) 

  • toFixed deals with decreasing accuracy. The number that looks “round” is not necessarily exact. Xkcd.ru/217 Suddenly the author needs accuracy up to a hundred characters? - andreymal
  • @andreymal Up to a hundred characters?)) I think the Number is not enough, except that developers.google.com/web/updates/2018/05/bigint - Alexander