1
0
forked from bonsai/harakit

hru(1): improved SI prefix logic

This commit is contained in:
Emma Tebibyte 2024-02-14 00:07:06 -07:00
parent 1299aefc39
commit e3a0069180
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270

View File

@ -27,13 +27,25 @@ extern crate sysexits;
use sysexits::{ EX_DATAERR, EX_SOFTWARE };
fn convert(input: u128) -> Result<(f64, u32), String> {
let list: Vec<u32> = vec![3, 6, 9, 12, 15, 18, 21, 24, 27, 30];
let mut out = (0.0, 0_u32);
const LIST: [(u32, &str); 10] = [
(3, "K"),
(6, "M"),
(9, "G"),
(12, "T"),
(15, "P"),
(18, "E"),
(21, "Z"),
(24, "Y"),
(27, "R"),
(30, "Q")
];
if input < 1000 { return Ok((input as f64, 0)); }
fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> {
for n in list {
let mut out = (input as f64, (0_u32, ""));
if input < 1000 { return Ok(out); }
for (n, p) in LIST {
let c = match 10_u128.checked_pow(n) {
Some(c) => c,
None => {
@ -43,10 +55,10 @@ fn convert(input: u128) -> Result<(f64, u32), String> {
match c.cmp(&input) {
Ordering::Less => {
out = (input as f64 / c as f64, n);
out = (input as f64 / c as f64, (n, p));
},
Ordering::Equal => {
return Ok((input as f64 / c as f64, n));
return Ok((input as f64 / c as f64, (n, p)));
},
Ordering::Greater => {},
};
@ -80,19 +92,7 @@ fn main() -> ExitCode {
},
};
let si_prefix = format!("{}B", match prefix {
3 => "K",
6 => "M",
9 => "G",
12 => "T",
15 => "P",
18 => "E",
21 => "Z",
24 => "Y",
27 => "R",
30 => "Q",
_ => "",
});
let si_prefix = format!("{}B", prefix.1);
let out = ((number * 10.0).round() / 10.0).to_string();