Minetest-WorldEditAdditions/worldeditadditions_core/utils/format/human_size.lua

15 lines
725 B
Lua
Raw Permalink Normal View History

--- Formats (usually large) numbers as human-readable strings.
-- Ported from PHP: https://github.com/sbrl/Pepperminty-Wiki/blob/0a81c940c5803856db250b29f54658476bc81e21/core/05-functions.php#L67
-- @param n number The number to format.
-- @param decimals number The number of decimal places to show.
-- @return string A formatted string that represents the given input number.
2022-09-18 16:32:13 +00:00
local function format_human_size(n, decimals)
2021-07-30 18:52:36 +00:00
local sizes = { "", "K", "M", "G", "T", "P", "E", "Y", "Z" }
local factor = math.floor((#tostring(n) - 1) / 3)
local multiplier = 10^(decimals or 0)
2022-09-17 22:42:46 +00:00
local result = math.floor(0.5 + (n / (1000 ^ factor)) * multiplier) / multiplier
return result .. sizes[factor+1]
end
2022-09-18 16:32:13 +00:00
return format_human_size