我一直在努力寻找一种很好的算法来将数字(可以是 float 或整数)更改为格式良好的人类可读数字,并将单位显示为字符串。例如:
100500000 -> '100.5 Mil'
200400 -> '200.4 K'
143000000 -> '143 Mil'
52000000000 -> '52 Bil'
等等,你明白了。
有什么建议吗?
最佳答案
我会改编下面的代码(我在网上找到的):
代码信用转到我发现的这个链接:http://www.phpfront.com/php/human-readable-byte-format/
function humanReadableOctets($octets)
{
$units = array('B', 'kB', 'MB', 'GB', 'TB'); // ...etc
for ($i = 0, $size =$octets; $size>1024; $size=$size/1024)
$i++;
return number_format($size, 2) . ' ' . $units[min($i, count($units) -1 )];
}
不过不要忘记将 1024 更改为 1000 ...
关于php - 将 120000000 转换为 '120 mil' 的好 PHP 算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/953523/