如何在PHP中格式化数字300到3.0

Ray*_*ond 2 php format numbers

我有一个包含整数的MySQL表.

Units
300,
900,
600,
125,
225,
Run Code Online (Sandbox Code Playgroud)

我希望它像这样使用PHP显示

Units
3.0,
9.0,
6.0,
1.25,
2.25,
Run Code Online (Sandbox Code Playgroud)

我尝试过使用number_format但是没有做到这一点.

Joh*_*nde 11

这是基本的数学:

echo $num * .01;
Run Code Online (Sandbox Code Playgroud)

如果你总是想要两个小数点:

echo number_format($num * .01, 2, '.', '');
Run Code Online (Sandbox Code Playgroud)

如果你想要x00数字的一个小数,只需使用第三个参数作为基于原始数字的最后两位数字的1或2变量,或使用模数运算符来确定该数字是否可被100整除.

$round = ($num % 100) ? 2 : 1;
echo number_format($num * .01, $round, '.', '');
Run Code Online (Sandbox Code Playgroud)

  • @Raymond***旁注:***如果您不知道Stack系统的工作原理,请阅读以下**http://meta.stackexchange.com/a/5235/**以了解如何接受回答,然后返回这里勾选复选标记.*欢迎来到Stack!* (2认同)