0

I'm trying to come up with a solution to avoid decimal errors when doing calculations in my application. I've seen some solutions but still have questions. My understanding is that floating point numbers can't be accurately stored in Javascript, numbers get a non 0 digit in their trails when they should be 0. Naughty digits, or digits that aren't noughty :) anyway...

eg. 0.1 + 0.2 = 0.xxxxxxx4

First question, would a solution be to not allow numbers to have > x decimal digits in any numbers as Javascript can't handle it? Limit the precision.

Next question, I have to compute strings like this: '2 + 2 + 3 * 0.1 + 0.2 * 10^5' how can I make sure it gets an accurate result? I was just going to use Mathjs but it won't avoid floats being wrong, calculations would be wrong wouldn't they? There are the solutions that use rounding that trims the trailing digits but how would I do that here? I'd have to write my own calculator that does rounding on every pair of numbers?

How can I decide what amount of decimal digits is acceptable? I'd guess the type of application is important, would 10 decimal digits be enough for most calculations, eg. finance? How many digits do calculators use when doing their calculations, is it just the amount you can see on the screen?

Next question, why doesn't Javascript limit the amount of decimal digits if it is going to have digits become numbers they aren't suppose to be? It's seems like 1 + 1 = 2....sorry no it doesn't. It's confusing.

coder
  • 11
  • 2
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – T.J. Crowder Jan 03 '20 at 12:48
  • Please ask **one** question per question, not three. You've asked three questions above. All of them have been asked and answered before, repeatedly, so it's a really good idea to [search](/help/searching) before posting. [Re dealing with precision issues](https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript); [re evaluating string math expressions](https://stackoverflow.com/questions/2276021/); [re why floating point is like this](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – T.J. Crowder Jan 03 '20 at 12:50
  • *"why doesn't Javascript limit the amount of decimal digits if it is going to have digits become numbers they aren't suppose to be"* Because it's not about the number of digits. The number `0.1` can't be accurately represented using IEEE-754 binary floating point (which is what JavaScript and many other languages use, for speed and memory efficiency). Which leads us to fun things like the famous `0.1 + 0.2` resulting in `0.30000000000000004`. – T.J. Crowder Jan 03 '20 at 12:51

0 Answers0