[英]PHP – Round number
This piece of code reads the size of a file, but the output for a file that is 1.759864Mb will be 1Mb and I'd like it to be 2Mb. 这段代码读取文件的大小,但是1.759864Mb文件的输出将是1Mb,我希望它是2Mb。 I know that it's about changing one little thing but I can't find what it is... 我知道这是关于改变一件小事,但我找不到它是什么......
function Size($path)
{
$bytes = sprintf('%u', filesize($path));
if ($bytes > 0)
{
$unit = intval(log($bytes, 1024));
$units = array('B', 'Kb', 'Mb', 'Gb');
if (array_key_exists($unit, $units) === true)
{
return sprintf('%d %s', $bytes / pow(1024, $unit), $units[$unit]);
}
}
return $bytes;
}
It should be 它应该是
return sprintf('%d %s', ceil($bytes / pow(1024, $unit)), $units[$unit]);
ceil
returns the next highest integer value by rounding up value if necessary. 如果需要, ceil
通过舍入值返回下一个最高整数值。
ceil($bytes / pow(1024, $unit));
我建议使用PHP round
函数而不是让sprintf
进行舍入(使用%d):
return sprintf('%d %s', round($bytes / pow(1024, $unit)), $units[$unit]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.