您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 临汾分类信息网,免费分类信息发布

全能的字节单位转换函数

2025/3/22 22:46:49发布15次查看
容量单位计算,支持定义小数保留长度;定义起始和目标单位,或按1024自动进位
class util {    /**     * 容量单位计算,支持定义小数保留长度;定义起始和目标单位,或按1024自动进位     *      * @param int $size,容量计数     * @param type $unit,容量计数单位,默认为字节     * @param type $decimals,小数点后保留的位数,默认保留一位     * @param type $targetunit,转换的目标单位,默认自动进位     * @return type 返回符合要求的带单位结果     */    static function filesizeconv($size, $unit = 'b', $decimals = 1, $targetunit = 'auto') {        $units = array('b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb');        $theunit = array_search(strtoupper($unit), $units); //初始单位是哪个        //判断是否自动计算,        if ($targetunit != 'auto')            $targetunit = array_search(strtoupper($targetunit), $units);        //循环计算        while ($size >= 1024) {            $size/=1024;            $theunit++;            if ($theunit == $targetunit)//已符合给定则退出循环吧!                break;        }        return sprintf(%1\$.{$decimals}f, $size) . $units[$theunit];    }}
复制代码
echo util::filesizeconv(6461310554654);结果:5.9tb
复制代码
临汾分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录