-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathnumber-formatting.rs
More file actions
34 lines (26 loc) · 953 Bytes
/
number-formatting.rs
File metadata and controls
34 lines (26 loc) · 953 Bytes
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
use std::io;
fn format_number(str: String) -> String {
let decimal_pos = str.find('.').unwrap();
let before = &str[..decimal_pos].trim_start_matches('0');
let after = &str[decimal_pos + 1..].trim_end_matches('0');
let filled = format!("{}{}{}{}",
"x".repeat(9 - before.len()),
before,
after,
"x".repeat(6 - after.len())
);
let mut result = String::new();
for (i, marker) in (0..filled.len()).step_by(3).zip( [",", ",", ".", ".", ""]) {
result.push_str(&filled[i..i+3]);
result.push_str(marker);
}
result
}
fn main() {
let mut input_line = String::new();
io::stdin().read_line(&mut input_line).unwrap();
let mut n = input_line.trim_matches('\n').replace('x', "0").to_string();
n.retain(|c: char| c.is_digit(10));
n.insert(9, '.');
println!("{}", format_number( format!("{:.6}", n.parse::<f64>().unwrap()/ 2f64) ));
}