2024-02-08 03:58:57 +00:00
|
|
|
|
/*
|
2024-02-08 04:42:43 +00:00
|
|
|
|
* Copyright (c) 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
2024-02-08 03:58:57 +00:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
|
* later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
|
* details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
|
cmp::Ordering,
|
|
|
|
|
env::args,
|
|
|
|
|
io::stdin,
|
2024-02-14 00:32:31 +00:00
|
|
|
|
process::ExitCode,
|
2024-02-08 03:58:57 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
extern crate sysexits;
|
|
|
|
|
|
2024-02-14 00:32:31 +00:00
|
|
|
|
use sysexits::EX_DATAERR;
|
2024-02-08 03:58:57 +00:00
|
|
|
|
|
2024-02-14 00:32:31 +00:00
|
|
|
|
fn convert(input: u64) -> (f64, u32) {
|
2024-02-08 03:58:57 +00:00
|
|
|
|
let list: Vec<u32> = vec![3, 6, 9, 12, 15, 18, 21, 24, 27, 30];
|
2024-02-14 00:32:31 +00:00
|
|
|
|
let mut out = (0.0, 0_u32);
|
2024-02-08 03:58:57 +00:00
|
|
|
|
|
2024-02-14 00:32:31 +00:00
|
|
|
|
if input < 1000 { return (input as f64, 0); }
|
|
|
|
|
|
|
|
|
|
for n in list {
|
|
|
|
|
let c = 10_u64.pow(n);
|
|
|
|
|
match c.cmp(&input) {
|
2024-02-08 03:58:57 +00:00
|
|
|
|
Ordering::Less => {
|
2024-02-14 00:32:31 +00:00
|
|
|
|
out = (input as f64 / c as f64, n);
|
2024-02-08 03:58:57 +00:00
|
|
|
|
},
|
2024-02-14 00:32:31 +00:00
|
|
|
|
Ordering::Equal => {
|
|
|
|
|
return (input as f64 / c as f64, n);
|
2024-02-08 03:58:57 +00:00
|
|
|
|
},
|
2024-02-14 00:32:31 +00:00
|
|
|
|
Ordering::Greater => {},
|
2024-02-08 03:58:57 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 00:32:31 +00:00
|
|
|
|
out
|
2024-02-08 03:58:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 00:32:31 +00:00
|
|
|
|
fn main() -> ExitCode {
|
2024-02-08 03:58:57 +00:00
|
|
|
|
let argv = args().collect::<Vec<String>>();
|
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
let _ = stdin().read_line(&mut buf);
|
|
|
|
|
|
|
|
|
|
let n: u64 = match buf.trim().parse() {
|
|
|
|
|
Ok(f) => f,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
eprintln!("{}: {}", argv[0], err);
|
2024-02-14 00:32:31 +00:00
|
|
|
|
return ExitCode::from(EX_DATAERR as u8);
|
2024-02-08 03:58:57 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-14 00:32:31 +00:00
|
|
|
|
let (number, prefix) = convert(n);
|
2024-02-08 03:58:57 +00:00
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
_ => "",
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-14 00:32:31 +00:00
|
|
|
|
let out = ((number * 10.0).round() / 10.0).to_string();
|
|
|
|
|
|
|
|
|
|
println!("{} {}", out, si_prefix);
|
|
|
|
|
|
|
|
|
|
ExitCode::SUCCESS
|
2024-02-08 03:58:57 +00:00
|
|
|
|
}
|