-
Notifications
You must be signed in to change notification settings - Fork 0
/
si.ex
153 lines (126 loc) · 3.39 KB
/
si.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
defmodule BetterNumber.SI do
@moduledoc """
Provides functions for formatting numbers using SI notation.
"""
@prefixes [
# yotta
{8, "Y"},
# zetta
{7, "Z"},
# exa
{6, "E"},
# peta
{5, "P"},
# tera
{4, "T"},
# giga
{3, "G"},
# mega
{2, "M"},
# kilo
{1, "k"},
{0, ""},
# milli
{-1, "m"},
# micro
{-2, "µ"},
# nano
{-3, "n"},
# pico
{-4, "p"},
# femto
{-5, "f"},
# atto
{-6, "a"},
# zepto
{-7, "z"},
# ycoto
{-8, "y"}
]
@defaults %{
base: 1000,
separator: "",
unit: "",
precision: 2,
trim: false
}
@doc """
Format numbers using SI notation
## Parameters
* `number` - A value to convert. Can be any value that implements
`BetterNumber.Conversion.to_float/1`.
* `options` - A keyword list of options. See the documentation below for all
available options.
## Options
* `:base` - Use 1024 if you wish to format bytes. Default: 1000
* `:separator` - The string to place between the scaled number and the `prefix
+ unit`. Perhaps you want a space here. Default: ""
* `:unit` - The unit of measurement, e.g. "M" for Meters. Default: ""
* `:precision` - The number of decimal places to include. Default: 2
* `:trim` - Trim trailing zeros. Default: false
## Examples
iex> BetterNumber.SI.number_to_si(1210000000, unit: "W")
"1.21GW"
iex> BetterNumber.SI.number_to_si(1210000000, unit: "W", precision: 1)
"1.2GW"
iex> BetterNumber.SI.number_to_si(1210000000, unit: "W", precision: 3, separator: " ")
"1.210 GW"
iex> BetterNumber.SI.number_to_si(1210000000, unit: "W", precision: 5, trim: true)
"1.21GW"
iex> BetterNumber.SI.number_to_si(1210000000)
"1.21G"
iex> BetterNumber.SI.number_to_si(Decimal.new(1210000000))
"1.21G"
"""
@spec number_to_si(BetterNumber.t(), map() | Keyword.t()) :: String.t()
def number_to_si(number, options \\ @defaults)
def number_to_si(number, options) when is_number(number) do
options = config(options)
exp = compute_exponent(number, options.base)
prefix = exponent_to_prefix(exp)
scaled_number = number / :math.pow(options.base, exp)
display_number = :erlang.float_to_binary(scaled_number, [{:decimals, options.precision}])
final_number = if options.trim, do: trim(display_number), else: display_number
final_number <> options.separator <> prefix <> options.unit
end
def number_to_si(number, options) do
number
|> BetterNumber.Conversion.to_float()
|> number_to_si(options)
end
defp compute_exponent(number, _) when number == 0, do: 0
defp compute_exponent(number, base) do
(:math.log(abs(number)) / :math.log(base))
|> Float.floor()
|> trunc()
|> max(-8)
|> min(8)
end
@doc false
for {num, text} = _p <- @prefixes do
def exponent_to_prefix(unquote(num)), do: unquote(text)
end
defp trim(display_number) do
if String.contains?(display_number, ".") do
display_number
|> String.trim_trailing("0")
|> String.trim_trailing(".")
else
display_number
end
end
defp config(
%{
base: _,
separator: _,
unit: _,
precision: _,
trim: _
} = options
) do
options
end
defp config(options) do
Map.merge(@defaults, Map.new(options))
end
end