多次加法后的Dart双十进制
尝试运行以下代码段时:多次加法后的Dart双十进制,dart,double,decimal,Dart,Double,Decimal,尝试运行以下代码段时: void main() { double step = 0.1; double currentAmount = 1; while (currentAmount < 2) { currentAmount = currentAmount + step; print (currentAmount); } } 有人能告诉我为什么小数是这样
void main() {
double step = 0.1;
double currentAmount = 1;
while (currentAmount < 2) {
currentAmount = currentAmount + step;
print (currentAmount);
}
}
有人能告诉我为什么小数是这样出现的吗?这是正常的行为吗?那是因为你正在失去精度 看看这个,它应该可以解决您的问题查看说明
1.1
1.2000000000000002
1.3000000000000003
1.4000000000000004
1.5000000000000004
1.6000000000000005
1.7000000000000006
1.8000000000000007
1.9000000000000008
2.000000000000001