转换最大单位字节将适合的字节数。
原型
size_format( int|string $bytes, int $decimals )
描述
读取1 KB比1024字节更容易,1 MB比1048576字节更容易。通过获取字节将进入的单位的数量,将字节数转换为人类可读的数字。支持TB值。
参数
$bytes
(int|string)
(Required)
字节数。注意整数的最大整数大小。
$decimals
(int)
(Optional)
小数位数的精度。默认值为0。
返回值
(string|false)
失败时是假的。成功的数字字符串。
源文件
路径:wp-includes/functions.php
<?php
...
function size_format( $bytes, $decimals = 0 ) {
$quant = array(
'TB' => TB_IN_BYTES,
'GB' => GB_IN_BYTES,
'MB' => MB_IN_BYTES,
'KB' => KB_IN_BYTES,
'B' => 1,
);
if ( 0 === $bytes ) {
return number_format_i18n( 0, $decimals ) . ' B';
}
foreach ( $quant as $unit => $mag ) {
if ( doubleval( $bytes ) >= $mag ) {
return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
}
}
return false;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/size_format/