0

In javascript console type in 79.99 * 100 instead of 7999 we get 7998.999999999999 This occurs only with specific digits(80.99,80.01) etc. Does not occur with 89.99,99.99 etc.

Is this a bug with Javascript?

Abhi.

Abhi
  • 55
  • 1
  • 10
  • 1
    No it's not a bug, it's been asked a million times and a quick search will reveal the answer. – Ian Jun 19 '14 at 07:59

1 Answers1

1

Yes. Javascript handles numbers differently and there's a problem with javascript number handler in some cases. This can be achieved by:

function strip(number) {
return (parseFloat(number.toPrecision(12)));
}

strip(79.99*100);
mehulmpt
  • 15,861
  • 12
  • 48
  • 88
  • *Differently?* From what? – Frédéric Hamidi Jun 19 '14 at 08:01
  • I read a javascript book in which it was written that the javascript engine handles floating numbers mechanism different as it looks. For example, 0.1+0.2 will give 0.30000000000004 but ((0.1*10)+(0.2*10))/10 will return 0.3 – mehulmpt Jun 19 '14 at 08:04
  • 1
    I see. Well, that's not specific to Javascript at all. It's actually more tied to the IEEE754 representation than to the language. – Frédéric Hamidi Jun 19 '14 at 08:06
  • Yep. The same is with ActionScript. – mehulmpt Jun 19 '14 at 08:06
  • And why is this specific to certain numbers? 79.99*100 has the issue where as 89.99 does not. Where do i find a list of such Magical numbers? Abhi. – Abhi Jun 19 '14 at 09:08