可能重複:我的程序的
How to iterate between 0.1f and 1.0f with 0.1f increments in Java?雙增量
部分需要使用while循環中值:
0.1
0.2
0.3
...
0.9
所以我需要爲他們提供的是內循環。 下面是代碼:
double x = 0.0;
while (x<=1)
{
// increment x by 0.1 for each iteration
x += 0.1;
}
我需要的輸出被精確地:
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
但它實際上給了我這樣的:
0.1
0.2
0.300000000000000000000000004
0.4
0.5
0.6
0.79999999999999999999999999
0.89999999999999999999999999
0.99999999999999999999999999
你似乎將正確的數字。我不明白爲什麼這不起作用。 – Mukus
@TejaswiRana:它不工作,因爲0.1實際上是0.0999999999999999999999或0.10000000000000000001。小數部分不能用double來無損地表示。 – cHao