如何在php中将数字300格式化为3.0


How to format number 300 to 3.0 in php

我有一个MySQL表,它包含整数。

Units
300,
900,
600,
125,
225,

我希望它能像一样使用PHP显示

Units
3.0,
9.0,
6.0,
1.25,
2.25,

我试过使用number_format,但没有成功。

这是基本数学:

echo $num * .01;

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

echo number_format($num * .01, 2, '.', '');

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

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