DEV Community

Discussion on: JavaScript's Broken Mathematics ? 💔💔

Collapse
 
darrylnoakes profile image
Darryl Noakes • Edited

The IEEE Floating Point standard used by almost all programming languages uses a rounding method known as "rounding ties to even" (also called "round to even" or "Banker's Rounding"):

  1. If the digit to be rounded off is less than 5, round down.
  2. If it is more than 5, round up.
  3. If it is equal to 5, look at the digit to the left. If that digit is odd, round up. If that digit is even, round down. From the SAS link: "In either case, the rounded number is an even integer."

This method preserves the average of a set of numbers more than the "half round up" method that most people use in day-to-day life.
It just skews the numbers towards even values a little.

So, in your case, you are multiplying 1.7885 by 10 and then rounding to 2 decimal places.
The digit to be rounded off is the 5.
Because it is 5, look at the digit on the left, which is 8.
8 is even, so we round down, leaving 17.88.

Hope this makes sense!

Relevant links:

mathsisfun.com/numbers/rounding-me... (really nice, and explains many methods)

blogs.sas.com/content/iml/2019/11/...

en.m.wikipedia.org/wiki/IEEE_754 (see "Rounding Rules")

en.m.wikipedia.org/wiki/Rounding#R...