0

Edit: found answer, not need to further comments/answer, thank you!

I have no idea what is going on in my script, and I know there is probably a question very similar to this but here goes:

console.log( ( 3 * 0.10 ) ); 
= 0.3000000000004 ( estimated the zeros, but close ) 

I tried parseInt() and I tried .1 and 0.1. Why doesn't it give me 0.3?.

console.log( ( 2 * 0.10 ) ); 
= 0.2 

Can someone help me with this?

Phil
  • 10,948
  • 17
  • 69
  • 101
  • 1
    Can you clarify what it is you are wanting as output? – DuckMaestro Jan 11 '13 at 22:13
  • Welcome to floating point world, fill that gap no one could call himself developer not knowing what is going on here – kidwon Jan 11 '13 at 22:14
  • 1
    This is a float point issue. Prior question asked on SO Here: http://stackoverflow.com/questions/1458633/elegant-workaround-for-javascript-floating-point-number-problem – Jack Jan 11 '13 at 22:14
  • @Phil No need to edit your post, just mark an answer as accepted when you are satisfied. – aaaaaaaaaaaa Jan 11 '13 at 22:23

3 Answers3

2

This happens because we can't map the whole range of real numbers. See w:floating point.

Zeta
  • 103,620
  • 13
  • 194
  • 236
  • 3
    @Phil It doesn't, standard float points can't represent `0.2` accurately, it just happens to be a case where the inaccuracy is rounded invisible for display. – aaaaaaaaaaaa Jan 11 '13 at 22:25
1

JavaScript uses binary floats. There is no way to represent 0.1 or 0.3 as binary number of finite length, just like it's impossible to write 1/3 as decimal number.

zch
  • 14,931
  • 2
  • 41
  • 49
  • That's not completely true. One could create a `fractional` object, which holds the denominator and enumerator. So there are ways to represent certain subsets of the real numbers, however those are only finite subsets. – Zeta Jan 11 '13 at 22:17
  • @Zeta, yes, you can also use BCD or something, but binary here means 'base-2 positional system' and not 'anything implemented with bits'. – zch Jan 11 '13 at 22:22
  • 1
    It doesn't, but due to rounding and perhaps due to artifacts of the implementation, it just displays better after decimal conversion. – DigitalRoss Jan 11 '13 at 22:28
1

This is what happens with binary representations of numbers. It is not possible to make an "exact" representation of 0.3 in binary, so when the computer converts the number, it will make the closest it can. In this case, not exactly 0.3 . You can get around this by formatting the output - using the .toFixed() method. In your case, if you want to output just one significant figure, you would do

var result
result = 3 * 0.10
console.log( result.toFixed(1) )
Floris
  • 45,857
  • 6
  • 70
  • 122
  • *"at w3schools.com - a great resource "* which is full of errors, see http://www.w3fools.com – Zeta Jan 12 '13 at 10:21
  • Removed reference to w3schools as "great resource", following the lead from Zeta. – Floris Jan 13 '13 at 14:01
  • how does this add to the other 2 answers? – Phil Jan 14 '13 at 14:04
  • @Phil, I wrote this "at the same time" as the others (so at the time I composed it I was "answering", not "adding"); plus I added a method to get the display down to the number of significant figures. – Floris Jan 14 '13 at 20:21
  • I apologize, this appeared quite a while after the other two! 1+ for `toFixed` – Phil Jan 14 '13 at 20:28