0

我正在使用一个简单的循环来回显一些数字

<?php
$inc = 0.25;
$start =  0.25;
$stop = 5.00;
?>

<?php while($start != ($stop + $inc)){ ?>
<option><?php echo $start ?></option>
<?php $start = $start + $inc; ?>
<?php } ?>

但是 5.00 显示为 5 而 4.50 显示为 4.5

我如何让这个脚本显示 5.00、4.00、3.00、3.50

还为标题道歉,我不知道如何解释!

谢谢

4

6 回答 6

6

用这个:

echo sprintf("%01.2f", $start)
于 2009-09-09T11:42:15.960 回答
4

还有 number_format 可以让您选择千位和小数分隔符: http: //de.php.net/manual/en/function.number-format.php

于 2009-09-09T12:09:27.630 回答
1

您将要printf()用于格式化字符串:

printf("%01.2f", $start)

(s)printf 的完整手册可以在这里找到

于 2009-09-09T11:43:34.750 回答
1
<?php while($start != ($stop + $inc)){ ?>
<option><?php printf('%01.2f', $start) ?></option>
<?php $start = $start + $inc; ?>
<?php } ?>
于 2009-09-09T11:46:07.693 回答
0

查看bcmath 库

于 2009-09-14T11:23:20.423 回答
0
$price1 = 6.67; $price2 = 5.55; $total = $price1 + $price2;
echo $total;
echo "\n";
//the . means dot position, 4 means 4 digits after the .
$result= sprint("%0.4f", $total); 
echo $result;
outputs: 12.22 12.2200

f 表示浮点数(也许很明显也许不是:)可以通过更改 . 得到你想要的任何位数。

于 2021-02-03T20:16:39.507 回答