Comparison

util/human/units.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parent 10903:c5f26f9adb31
child 12573:0f4feaf9ca64
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
1 local math_abs = math.abs;
2 local math_ceil = math.ceil;
3 local math_floor = math.floor;
4 local math_log = math.log;
5 local math_max = math.max;
6 local math_min = math.min;
7 local unpack = table.unpack or unpack; --luacheck: ignore 113
8
9 if math_log(10, 10) ~= 1 then
10 -- Lua 5.1 COMPAT
11 local log10 = math.log10;
12 function math_log(n, base)
13 return log10(n) / log10(base);
14 end
15 end
16
17 local large = {
18 "k", 1000,
19 "M", 1000000,
20 "G", 1000000000,
21 "T", 1000000000000,
22 "P", 1000000000000000,
23 "E", 1000000000000000000,
24 "Z", 1000000000000000000000,
25 "Y", 1000000000000000000000000,
26 }
27 local small = {
28 "m", 0.001,
29 "μ", 0.000001,
30 "n", 0.000000001,
31 "p", 0.000000000001,
32 "f", 0.000000000000001,
33 "a", 0.000000000000000001,
34 "z", 0.000000000000000000001,
35 "y", 0.000000000000000000000001,
36 }
37
38 local binary = {
39 "Ki", 2^10,
40 "Mi", 2^20,
41 "Gi", 2^30,
42 "Ti", 2^40,
43 "Pi", 2^50,
44 "Ei", 2^60,
45 "Zi", 2^70,
46 "Yi", 2^80,
47 }
48
49 local function adjusted_unit(n, b)
50 local round = math_floor;
51 local prefixes = large;
52 local logbase = 1000;
53 if b == 'b' then
54 prefixes = binary;
55 logbase = 1024;
56 elseif n < 1 then
57 prefixes = small;
58 round = math_ceil;
59 end
60 local m = math_max(0, math_min(8, round(math_abs(math_log(math_abs(n), logbase)))));
61 local prefix, multiplier = unpack(prefixes, m * 2-1, m*2);
62 return multiplier or 1, prefix;
63 end
64
65 -- n: number, the number to format
66 -- unit: string, the base unit
67 -- b: optional enum 'b', thousands base
68 local function format(n, unit, b) --> string
69 local fmt = "%.3g %s%s";
70 if n == 0 then
71 return fmt:format(n, "", unit);
72 end
73 local multiplier, prefix = adjusted_unit(n, b);
74 return fmt:format(n / multiplier, prefix or "", unit);
75 end
76
77 return {
78 adjust = adjusted_unit;
79 format = format;
80 };