返回列表

javascript 问题 之 0.1 + 0.2 != 0.3

默认分类 2014/07/02 01:09

参考地址:http://lifesinger.wordpress.com/2011/03/07/js-precision/

0.05 + 0.01 = 0.060000000000000005
0.06 + 0.01 = 0.06999999999999999
0.09 + 0.01 = 0.09999999999999999
0.14 + 0.01 = 0.15000000000000002
0.17 + 0.01 = 0.18000000000000002
0.2 + 0.01 = 0.21000000000000002
0.23 + 0.01 = 0.24000000000000002
0.28 + 0.01 = 0.29000000000000003
0.34 + 0.01 = 0.35000000000000003
0.35000000000000003 + 0.01 = 0.36000000000000004
0.4 + 0.01 = 0.41000000000000003
0.41000000000000003 + 0.01 = 0.42000000000000004
0.46 + 0.01 = 0.47000000000000003
0.47000000000000003 + 0.01 = 0.48000000000000003
0.56 + 0.01 = 0.5700000000000001
0.5700000000000001 + 0.01 = 0.5800000000000001
0.68 + 0.01 = 0.6900000000000001
0.6900000000000001 + 0.01 = 0.7000000000000001
0.7000000000000001 + 0.01 = 0.7100000000000001
0.81 + 0.01 = 0.8200000000000001
0.8200000000000001 + 0.01 = 0.8300000000000001
0.8300000000000001 + 0.01 = 0.8400000000000001
0.93 + 0.01 = 0.9400000000000001
0.9400000000000001 + 0.01 = 0.9500000000000001
0.9500000000000001 + 0.01 = 0.9600000000000001

0.07 + 0.02 = 0.09000000000000001
0.1 + 0.02 = 0.12000000000000001
0.12 + 0.02 = 0.13999999999999998
0.15 + 0.02 = 0.16999999999999998
0.18 + 0.02 = 0.19999999999999998
0.21 + 0.02 = 0.22999999999999998
0.27 + 0.02 = 0.29000000000000003
0.28 + 0.02 = 0.30000000000000004
0.33 + 0.02 = 0.35000000000000003
0.34 + 0.02 = 0.36000000000000004
0.35000000000000003 + 0.02 = 0.37000000000000005
0.39 + 0.02 = 0.41000000000000003
0.4 + 0.02 = 0.42000000000000004
0.41000000000000003 + 0.02 = 0.43000000000000005
0.45 + 0.02 = 0.47000000000000003
0.46 + 0.02 = 0.48000000000000003
0.47000000000000003 + 0.02 = 0.49000000000000004
0.55 + 0.02 = 0.5700000000000001
0.56 + 0.02 = 0.5800000000000001
0.5700000000000001 + 0.02 = 0.5900000000000001
0.67 + 0.02 = 0.6900000000000001
0.68 + 0.02 = 0.7000000000000001
0.6900000000000001 + 0.02 = 0.7100000000000001
0.7000000000000001 + 0.02 = 0.7200000000000001
0.8 + 0.02 = 0.8200000000000001
0.81 + 0.02 = 0.8300000000000001
0.8200000000000001 + 0.02 = 0.8400000000000001
0.8300000000000001 + 0.02 = 0.8500000000000001
0.92 + 0.02 = 0.9400000000000001
0.93 + 0.02 = 0.9500000000000001
0.9400000000000001 + 0.02 = 0.9600000000000001
0.9500000000000001 + 0.02 = 0.9700000000000001

十进制转二进制
整数部分 商除二取余 如: 10 = 1010 小数部分 积乘二取整 如: 0.25 = 0.01, 0.3 = 0.010011001...1001...

1.0000000000000001 == 1