标签:
double opacity = 0; for (int i = 0; i < 100; i++) { opacity = ((int) (opacity * 100 + 1)) / 100.0; System.out.println("opacity=" + opacity); }
opacity=0.01
opacity=0.02
opacity=0.03
opacity=0.04
opacity=0.05
opacity=0.06
(中间省略……)
opacity=0.27
opacity=0.28
opacity=0.29
opacity=0.29
opacity=0.29
……后面一直为0.29
现在我把上面那个程序做一点修改:
double opacity = 0; for (int i = 0; i < 100; i++) { opacity = ((int) (opacity * 100 + 1)) / 100.0; System.out.println("opacity=" + new BigDecimal(opacity)); System.out.println("opacity*100=" + new BigDecimal(opacity * 100)); System.out.println("----------------------------"); }
opacity=0.01000000000000000020816681711721685132943093776702880859375opacity*100=1----------------------------opacity=0.0200000000000000004163336342344337026588618755340576171875opacity*100=2----------------------------opacity=0.0299999999999999988897769753748434595763683319091796875opacity*100=3(中间省略……)opacity=0.270000000000000017763568394002504646778106689453125opacity*100=27----------------------------opacity=0.2800000000000000266453525910037569701671600341796875opacity*100=28.000000000000003552713678800500929355621337890625----------------------------opacity=0.289999999999999980015985556747182272374629974365234375opacity*100=28.999999999999996447286321199499070644378662109375……后面一直重复相同的内容
程序的目的是让opacity的值每次增加0.01,那么就只需要每次加上0.01就好了,完全不需要绕圈子。如下:
double opacity = 0; while (opacity < 1) { opacity += 0.01; System.out.println("opacity=" + opacity); }
opacity=0.01
opacity=0.02
opacity=0.03
opacity=0.04
opacity=0.05
opacity=0.060000000000000005
(中间省略……)
opacity=0.9800000000000006
opacity=0.9900000000000007
opacity=1.0000000000000007
double opacity = 0; for (int i = 0; i < 100; i++) { opacity = ((int) (opacity * 100 + 1.5)) / 100.0; System.out.println("opacity=" + opacity); }
opacity=0.01
opacity=0.02
opacity=0.03
opacity=0.04
opacity=0.05
opacity=0.06
(中间省略……)
opacity=0.96
opacity=0.97
opacity=0.98
opacity=0.99
opacity=1.0
标签:
原文地址:http://www.cnblogs.com/antineutrino/p/4525224.html