1

Possible Duplicate:
Is JavaScript's Math broken?

I've some problems with javascript.

If I do this:

alert(6.000 * 1.050);

I expect 6.3, but I get 6.30000001

can anybody help me ? or explain why this happens?

Community
  • 1
  • 1
Ruutert
  • 395
  • 1
  • 7
  • 18
  • 1
    That's just down to how floating point numbers are represented and is not limited to javascript. For details, read [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – Shawn Chin Dec 09 '11 at 09:48

3 Answers3

1

Here you can simple use the method toFixed() in java-script

alert(parseFloat(6.000 * 1.050).toFixed(1));
Sreekumar P
  • 5,900
  • 11
  • 57
  • 82
0

They're called floats, and sometimes have a little bit of inaccuracy.

http://en.wikipedia.org/wiki/Floating_point#Accuracy_proble

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
0

Standard problem; decimals can't be stored with infinite precision in general, so most programming languages have data types that approximates them and show a rounded version. Problem is that multiplication or subtraction can cause the inaccuracies to show.

In the end, you'll just have to round probably.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Johan
  • 3,202
  • 1
  • 23
  • 19