Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_datime/src/constant/si_prefix.rs

5.9 KiB, 17 runs

created by r1870400018:8426, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! SI (Système International) unit prefixes.
2//!
3//! Refer to https://en.wikipedia.org/wiki/Metric_prefix
4//!
5//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
6//! Anthropic Claude
7
8use oxedyne_fe2o3_core::prelude::*;
9
10#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
11pub enum SIPrefix {
12 Yotta,
13 Zetta,
14 Exa,
15 Peta,
16 Tera,
17 Giga,
18 Mega,
19 Kilo,
20 Hecto,
21 Deca,
22 Deci,
23 Centi,
24 Milli,
25 Micro,
26 Nano,
27 Pico,
28 Femto,
29 Atto,
30 Zepto,
31 Yocto,
32}
33
34impl SIPrefix {
35 pub fn to_symbol(&self) -> &'static str {
36 match self {
37 Self::Yotta => "Y",
38 Self::Zetta => "Z",
39 Self::Exa => "E",
40 Self::Peta => "P",
41 Self::Tera => "T",
42 Self::Giga => "G",
43 Self::Mega => "M",
44 Self::Kilo => "k",
45 Self::Hecto => "h",
46 Self::Deca => "da",
47 Self::Deci => "d",
48 Self::Centi => "c",
49 Self::Milli => "m",
50 Self::Micro => "u",
51 Self::Nano => "n",
52 Self::Pico => "p",
53 Self::Femto => "f",
54 Self::Atto => "a",
55 Self::Zepto => "z",
56 Self::Yocto => "y",
57 }
58 }
59
60 pub fn to_log10(&self) -> i8 {
61 match self {
62 Self::Yotta => 24,
63 Self::Zetta => 21,
64 Self::Exa => 18,
65 Self::Peta => 15,
66 Self::Tera => 12,
67 Self::Giga => 9,
68 Self::Mega => 6,
69 Self::Kilo => 3,
70 Self::Hecto => 2,
71 Self::Deca => 1,
72 Self::Deci => -1,
73 Self::Centi => -2,
74 Self::Milli => -3,
75 Self::Micro => -6,
76 Self::Nano => -9,
77 Self::Pico => -12,
78 Self::Femto => -15,
79 Self::Atto => -18,
80 Self::Zepto => -21,
81 Self::Yocto => -24,
82 }
83 }
84
85 /// Short scale, as used in American English.
86 pub fn to_short_name(&self) -> &'static str {
87 match self {
88 Self::Yotta => "septillion",
89 Self::Zetta => "sextillion",
90 Self::Exa => "quintillion",
91 Self::Peta => "quadrillion",
92 Self::Tera => "trillion",
93 Self::Giga => "billion",
94 Self::Mega => "million",
95 Self::Kilo => "thousand",
96 Self::Hecto => "hundred",
97 Self::Deca => "ten",
98 Self::Deci => "tenth",
99 Self::Centi => "hundredth",
100 Self::Milli => "thousandth",
101 Self::Micro => "millionth",
102 Self::Nano => "billionth",
103 Self::Pico => "trillionth",
104 Self::Femto => "quadrillionth",
105 Self::Atto => "quintillionth",
106 Self::Zepto => "sextillionth",
107 Self::Yocto => "septillionth",
108 }
109 }
110
111 /// Long scale, as used in European English.
112 pub fn to_long_name(&self) -> &'static str {
113 match self {
114 Self::Yotta => "quadrillion",
115 Self::Zetta => "trilliard",
116 Self::Exa => "trillion",
117 Self::Peta => "billiard",
118 Self::Tera => "billion",
119 Self::Giga => "milliard",
120 Self::Mega => "million",
121 Self::Kilo => "thousand",
122 Self::Hecto => "hundred",
123 Self::Deca => "ten",
124 Self::Deci => "tenth",
125 Self::Centi => "hundredth",
126 Self::Milli => "thousandth",
127 Self::Micro => "millionth",
128 Self::Nano => "milliardth",
129 Self::Pico => "billionth",
130 Self::Femto => "billiardth",
131 Self::Atto => "trillionth",
132 Self::Zepto => "trilliardth",
133 Self::Yocto => "quadrillionth",
134 }
135 }
136
137 pub fn get_using_symbol(symbol: &str) -> Option<Self> {
138 let symbol = symbol.trim();
139 match symbol {
140 "Y" => Some(Self::Yotta),
141 "Z" => Some(Self::Zetta),
142 "E" => Some(Self::Exa),
143 "P" => Some(Self::Peta),
144 "T" => Some(Self::Tera),
145 "G" => Some(Self::Giga),
146 "M" => Some(Self::Mega),
147 "k" => Some(Self::Kilo),
148 "h" => Some(Self::Hecto),
149 "da" => Some(Self::Deca),
150 "d" => Some(Self::Deci),
151 "c" => Some(Self::Centi),
152 "m" => Some(Self::Milli),
153 "u" => Some(Self::Micro),
154 "n" => Some(Self::Nano),
155 "p" => Some(Self::Pico),
156 "f" => Some(Self::Femto),
157 "a" => Some(Self::Atto),
158 "z" => Some(Self::Zepto),
159 "y" => Some(Self::Yocto),
160 _ => None,
161 }
162 }
163
164 pub fn get_using_log10(log10: i8) -> Option<Self> {
165 match log10 {
166 24 => Some(Self::Yotta),
167 21 => Some(Self::Zetta),
168 18 => Some(Self::Exa),
169 15 => Some(Self::Peta),
170 12 => Some(Self::Tera),
171 9 => Some(Self::Giga),
172 6 => Some(Self::Mega),
173 3 => Some(Self::Kilo),
174 2 => Some(Self::Hecto),
175 1 => Some(Self::Deca),
176 -1 => Some(Self::Deci),
177 -2 => Some(Self::Centi),
178 -3 => Some(Self::Milli),
179 -6 => Some(Self::Micro),
180 -9 => Some(Self::Nano),
181 -12 => Some(Self::Pico),
182 -15 => Some(Self::Femto),
183 -18 => Some(Self::Atto),
184 -21 => Some(Self::Zepto),
185 -24 => Some(Self::Yocto),
186 _ => None,
187 }
188 }
189
190 pub fn get_using_short_name(name: &str) -> Option<Self> {
191 let name = name.trim().to_lowercase();
192 match name.as_str() {
193 "septillion" => Some(Self::Yotta),
194 "sextillion" => Some(Self::Zetta),
195 "quintillion" => Some(Self::Exa),
196 "quadrillion" => Some(Self::Peta),
197 "trillion" => Some(Self::Tera),
198 "billion" => Some(Self::Giga),
199 "million" => Some(Self::Mega),
200 "thousand" => Some(Self::Kilo),
201 "hundred" => Some(Self::Hecto),
202 "ten" => Some(Self::Deca),
203 "tenth" => Some(Self::Deci),
204 "hundredth" => Some(Self::Centi),
205 "thousandth" => Some(Self::Milli),
206 "millionth" => Some(Self::Micro),
207 "billionth" => Some(Self::Nano),
208 "trillionth" => Some(Self::Pico),
209 "quadrillionth" => Some(Self::Femto),
210 "quintillionth" => Some(Self::Atto),
211 "sextillionth" => Some(Self::Zepto),
212 "septillionth" => Some(Self::Yocto),
213 _ => None,
214 }
215 }
216
217 pub fn get_using_long_name(name: &str) -> Option<Self> {
218 let name = name.trim().to_lowercase();
219 match name.as_str() {
220 "quadrillion" => Some(Self::Yotta),
221 "trilliard" => Some(Self::Zetta),
222 "trillion" => Some(Self::Exa),
223 "billiard" => Some(Self::Peta),
224 "billion" => Some(Self::Tera),
225 "milliard" => Some(Self::Giga),
226 "million" => Some(Self::Mega),
227 "thousand" => Some(Self::Kilo),
228 "hundred" => Some(Self::Hecto),
229 "ten" => Some(Self::Deca),
230 "tenth" => Some(Self::Deci),
231 "hundredth" => Some(Self::Centi),
232 "thousandth" => Some(Self::Milli),
233 "millionth" => Some(Self::Micro),
234 "milliardth" => Some(Self::Nano),
235 "billionth" => Some(Self::Pico),
236 "billiardth" => Some(Self::Femto),
237 "trillionth" => Some(Self::Atto),
238 "trilliardth" => Some(Self::Zepto),
239 "quadrillionth" => Some(Self::Yocto),
240 _ => None,
241 }
242 }
243}