dtq81142 2015-03-29 18:33
浏览 99
已采纳

对于循环增加3小数点PHP

I need a loop to increase the number by 3rd decimal and the print should look like this

1.1
1.101
1.102
1.103
...
2.0
2.001
2.002
...
2.1
2.101
...

for ($i = 1.1; $i <= 3; $i += .001){

   echo $i . '<br />';
}

the start seems to be ok but somewhere along the loop the decimal number becomes to big

1.54
1.541
1.542
1.543
1.544
1.545
1.546
1.547
1.548
1.549
1.55
1.551
1.552
1.553
1.554
1.5549999999999
1.5559999999999
1.5569999999999
1.5579999999999
1.5589999999999

And if I use

number_format($i,3)

all my numbers have 3 decimal points like

1.100 and I need it to be 1.1 .

What am I missing ?

  • 写回答

4条回答 默认 最新

  • dqtiober37522 2015-03-29 18:38
    关注

    Live demo

    Try rounding the values and store them in $i:

    for ($i = 1.1; $i <= 3; $i += .001){
        $i = round($i, 3);
       echo $i . '<br />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?