可以echo指定一个浮点数


Can echo specify a floating-point number?

我可以使用printf()

printf ("Total amount of order is %.2f", $total);

我可以使用echo做同样的事情吗,或者我必须使用printf()?

单独使用echo ?不。您可以在返回的字符串中包含number_format以获得相同的功能。

echo "Total amount of order is ".number_format($total,2);

不能使用echo指定格式。

然而,如果你需要将结果分配给字符串,你可以简单地使用sprintf函数:

$var = sprintf ("Total amount of order is %.2f", $total);
echo $var;

您可以使用…

$total= 100.123;
echo "Total amount of order is ". round($total, 2);

:

Total amount of order is 100.12