标签:throw 流量 orm set cep while 转换 format for
#格式化流量
public static function formatFlux($flux) { $unit = [‘MB‘, ‘GB‘, ‘TB‘, ‘PB‘, ‘EB‘, ‘ZB‘, ‘YB‘, ‘BB‘, ‘NB‘, ‘DB‘]; $i = 0; while (true) { if ($flux<1024 && isset($unit[$i])) { return [‘number‘ => bcmul($flux, 1, 1), ‘unit‘ => $unit[$i]]; } $i++; if (!isset($unit[$i])) { return [‘number‘ => bcmul($flux, 1, 1), ‘unit‘ => $unit[$i-1]]; } $flux = bcdiv($flux, 1024, 10); } }
#将流量转为MB
public static function convertToMB($traffic, $unit) { $units = [‘MB‘, ‘GB‘, ‘TB‘, ‘PB‘, ‘EB‘, ‘ZB‘, ‘YB‘, ‘BB‘, ‘NB‘, ‘DB‘]; if (!in_array($unit, $units, true)) { throw new \Exception(‘转换单位错误‘); } $index = array_search($unit, $units); return $index==0 ? $traffic : bcmul(bcpow(1024, $index), $traffic, 2); }
标签:throw 流量 orm set cep while 转换 format for
原文地址:https://www.cnblogs.com/2019PawN/p/12258213.html