PHP 文件大小转换器
PHP file size converter
我需要将随机数转换为 KB、MB、GB 和 TB。比如生成2048,需要显示为2KB。
我不知道从哪里开始,除了生成一个随机数:
$number = rand(1,1000000);
echo $number;
如果有人指出我正确的方向,我将不胜感激。
您可以round()
,以下内容可能对您有所帮助。我在生产中使用这段代码很长时间了。
function convert_bytes_to_hr_format($size){
if (1024 > $size) {
return $size.' B';
} else if (1048576 > $size) {
return round( ($size / 1024) , 2). ' KB';
} else if (1073741824 > $size) {
return round( (($size / 1024) / 1024) , 2). ' MB';
} else if (1099511627776 > $size) {
return round( ((($size / 1024) / 1024) / 1024) , 2). ' GB';
}
}
我需要将随机数转换为 KB、MB、GB 和 TB。比如生成2048,需要显示为2KB。
我不知道从哪里开始,除了生成一个随机数:
$number = rand(1,1000000);
echo $number;
如果有人指出我正确的方向,我将不胜感激。
您可以round()
,以下内容可能对您有所帮助。我在生产中使用这段代码很长时间了。
function convert_bytes_to_hr_format($size){
if (1024 > $size) {
return $size.' B';
} else if (1048576 > $size) {
return round( ($size / 1024) , 2). ' KB';
} else if (1073741824 > $size) {
return round( (($size / 1024) / 1024) , 2). ' MB';
} else if (1099511627776 > $size) {
return round( ((($size / 1024) / 1024) / 1024) , 2). ' GB';
}
}