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