Saturday, November 7, 2015

Best Practice: Watchout Wacky Decimals in JavaScript

Make your Calculations Consistent

JS uses binary floating point to handle decimal based operations.
JavaScript calculations with floating point causes the Nonsense cases especially during the result comparisons & displaying.
Here are few points how to start doing better:
  • round your values before displaying;
  • always use radix, to ensure that result is in the number system you expect;
  • test for number presence before doing math operations;
  • remember isNaN() is looking strictly for NaN;
  • parse your string numerical data before calculations.

No comments:

Post a Comment