PHP数学问题

0 php format math rounding

让我说我将14 / 15它除以100来获得百分比,这是93.33333333333333如何93.3%使用PHP 显示它?

这是代码.

$percent = ($avg / 15) * 100;
Run Code Online (Sandbox Code Playgroud)

Wim*_*Wim 8

sprintf功能是为此而作出的(另见手册):

echo sprintf('%.1f%%', $percent);
Run Code Online (Sandbox Code Playgroud)

  • 根据您的需求,round()(http://php.net/manual/en/function.round.php)也是一个不错的选择. (2认同)

Dan*_*uis 8

PHP有一个number_format函数,它允许您指定小数位数,小数点分隔符的用途以及千位分隔符的用途:

$percent = ($avg / 15) * 100;
echo number_format($percent, 1) . '%'; // => 93.3%
Run Code Online (Sandbox Code Playgroud)