|
补一个大小转单位的辅助函数。
下午更新一下。
Format = {}
Fmt = Format
function Format.SizeUnit(size, fmt)
local units = {"Bytes", "KB", "MB", "GB", "TB"}
local n = 1
local quot = size
while (quot >= 1024) do
n = n + 1
quot = quot / 1024
if n == #units then break end
end
return string.format(fmt, quot) .. units[n], quot, units[n]
end
-- test
print(Format.SizeUnit(0, "%.0f ")) -- 0 Bytes
print(Format.SizeUnit(123, "%.0f ")) -- 123 Bytes
print(Format.SizeUnit(1024, "%.0f ")) -- 1 KB
print(Format.SizeUnit(20250505, "%.0f ")) -- 19 MB
print(Format.SizeUnit(20250505, "%.2f ")) -- 19.31 MB
print(Fmt.SizeUnit(0x1ffc00000, "%.0f ")) -- 8 GB
print(Fmt.SizeUnit(1500000000000, "%.2f ")) -- 1.36 TB
print(Fmt.SizeUnit(1500000000000000, "%.0f ")) -- 1364 TB
|
|