문제

Well i just got a problem, with the simple following code:

trace( 0.01+0.05 );  // 0.060000000000000005
trace( 0.03-0.01 );  // 0.019999999999999997

I mean i just want 0.01+0.05 give me 0.06 and 0.03-0.01 give me 0.02. Does someone have an idea how to retrieve the correct results ?

도움이 되었습니까?

해결책

The imprecision is due to floating point arithmetic. 0.01, 0.05 and 0.03 are all floating point literals. Not every number (in fact, very few numbers) can be represented precisely in floating point.

For example, 0.5 can be but, 0.06 cannot. As a rule of thumb the first 15 significant figures will be correct.

For more details, see http://en.wikipedia.org/wiki/Floating_point

다른 팁

trace(Math.format((0.01+0.05), 2));

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top