Annotate

util/human/units.lua @ 10886:994c4a333199

util.human.units: A library for formatting numbers with SI units
author Kim Alvefur <zash@zash.se>
date Fri, 04 Jan 2019 08:46:26 +0100
child 10888:42a0d9089de9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10886
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local large = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 "k", 1000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 "M", 1000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 "G", 1000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 "T", 1000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 "P", 1000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 "E", 1000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 "Z", 1000000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 "Y", 1000000000000000000000000,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local small = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 "m", 0.001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 "μ", 0.000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 "n", 0.000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 "p", 0.000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 "f", 0.000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 "a", 0.000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 "z", 0.000000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 "y", 0.000000000000000000000001,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local binary = {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 "Ki", 2^10,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 "Mi", 2^20,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 "Gi", 2^30,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 "Ti", 2^40,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 "Pi", 2^50,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 "Ei", 2^60,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 "Zi", 2^70,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 "Yi", 2^80,
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 }
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 -- n: number, the number to format
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 -- unit: string, the base unit
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 -- b: optional enum 'b', thousands base
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local function format(n, unit, b) --> string
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 local round = math.floor;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local prefixes = large;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 local logbase = 1000;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local fmt = "%.3g %s%s";
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 if n == 0 then
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 return fmt:format(n, "", unit);
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 if b == 'b' then
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 prefixes = binary;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 logbase = 1024;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 elseif n < 1 then
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 prefixes = small;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 round = math.ceil;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 end
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 local m = math.max(0, math.min(8, round(math.abs(math.log(math.abs(n), logbase)))));
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local prefix, multiplier = table.unpack(prefixes, m * 2-1, m*2);
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 return fmt:format(n / (multiplier or 1), prefix or "", unit);
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 return {
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 format = format;
994c4a333199 util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 };