我正在尝试添加一长串我定义为浮点数的数字。数字显示为自爆,如图所示,它在添加数字时会添加一些额外的分数,如何避免这些?我应该使用哪种数据类型?
float number;
float sum;
....{
........
System.out.println(number);
sum = sum + number;
System.out.println("sum:" + sum);
}
结果
3.96
sum:3.96
5.04
sum:9.0
5.04
sum:14.04
2.16
sum:16.2
1.44
sum:17.640001
3.96
sum:21.600002
3.96
sum:25.560001
2.88
sum:28.440002
10.26
sum:38.700005
1.62
sum:40.320004
3.01
sum:43.33
1.8
sum:45.13
1.98
sum:47.11
1.935
sum:49.045002
3.96
sum:53.005
1.44
sum:54.445
1.44
sum:55.885
1.44
sum:57.324997
6.48
sum:63.804996
4.3
sum:68.104996