3

When i assign 99999999999.999999 to a variable, it gets round off to 100000000000, and when i remove one 9 from the value , the last digit becomes 8.

like,

9999999999.999999 = 9999999999.999998
99999999999.99999 = 99999999999.99998

kindly help me to resolve this

Thorin Oakenshield
  • 14,232
  • 33
  • 106
  • 146
  • the floating piont error – HIRA THAKUR Nov 07 '13 at 11:08
  • 3
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – adeneo Nov 07 '13 at 11:10
  • This is because how numbers are saved on a computer. http://en.wikipedia.org/wiki/Computer_number_format You can use big decimal for such purposes http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html – Mirco Nov 07 '13 at 11:11
  • @verbose-mode: This is javascript not java, they cannot use BigDecimal (not that java version anyway) – musefan Nov 07 '13 at 11:13

1 Answers1

2

Numbers in ECMAScript (Javascript) are internally represented by double-precision floating-point. When setting a number, it actually is assigned the nearest representable double-precision value, which is 100000000000 in this case.

See Large numbers erroneously rounded in Javascript

Community
  • 1
  • 1
Willem Mulder
  • 12,974
  • 3
  • 37
  • 62