3

I have a problem with rounding numbers.

x = 0.175;
console.log(x.toFixed(2));
// RESULT: 0.17

x = 1.175;
console.log(x.toFixed(2));
// RESULT: 1.18

x = 2.175;
console.log(x.toFixed(2));
// RESULT: 2.17

Why is (X!=1).175 not rounded to X.18?

j08691
  • 204,283
  • 31
  • 260
  • 272
Jean Paul CP
  • 253
  • 2
  • 10

4 Answers4

1

The problem here is that 0.175 is a repeating decimal in binary (specifically, after a short prefix, it settles down to a repeating 0011 pattern). When represented in a finite floating point representation, this repeating pattern gets truncated. When you change the integer part from 0 to 1 to 2, you are adding one additional bit each time to the integer part of the number, which pushes off one trailing bit. Depending on what bit value gets pushed off, that can change the rounded value enough to affect the visible result. Note that after 2.175, the next change in rounding behavior doesn't occur until 8.175 (after two more low-order bits have been pushed off the representation).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

This is the Reason behind this...

Squeezing infinitely many real numbers are into a finite number of bits requires an approximate representation.

Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits.

In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation.

x = 0.175;
console.log(x.toFixed(20));
// RESULT: 0.17

x = 1.175;
console.log(x.toFixed(20));
// RESULT: 1.18

x = 2.175;
console.log(x.toFixed(20));
// RESULT: 2.17

This rounding error is the characteristic feature of floating-point computation.

Source : http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Sankar
  • 6,908
  • 2
  • 30
  • 53
0

JavaScript has plenty of rounding problems, it's the result of binary machines trying to represent fractions in a decimal number system. There's always inaccuracies. Sometimes, a 5 is rounded up and other times it is rounded down. It's talked about in these articles or topics:

http://www.jacklmoore.com/notes/rounding-in-javascript/

Avoiding problems with JavaScript's weird decimal calculations

How to deal with floating point number precision in JavaScript?

Even a more precise control of floating point representation in JavaScript doesn't fix the issue:

> x=2175e-3; x.toFixed(2);
  "2.17"
> x=1175e-3; x.toFixed(2);
  "1.18"

In cases where it's super important to get predictable results, at least one of these articles suggest using a technique "epsilon estimation," which actually is the heart of several definitions in calculus. To learn that fix is to probably learn a lot more than you bargained for.

Community
  • 1
  • 1
SomeDude
  • 320
  • 2
  • 7
-3

JavaScript Numbers are Always 64-bit Floating Point.

Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.

JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.

The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:

Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41