From f4a5fbd1d64e3e013ce8ca5776805ee5fd7a58e8 Mon Sep 17 00:00:00 2001 From: Vla Mai Date: Tue, 2 Apr 2019 06:30:13 +0500 Subject: add thousend separator print --- src/main.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 94cbc1e..3b8ab6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,47 @@ fn main() { } fn pprint(ans: f64) { - println!("{a:width$}", width = CONFIGURATION.fix, a = ans) + // remove "-" char to add it later + let negative = ans < 0.0; + let ans = ans.abs(); + + let mut result_string = String::new(); + let frac_part = ans.fract(); + let i_part = ans - frac_part; + + for (i,c) in i_part.to_string().chars().rev().enumerate(){ + if i % 3 == 0 && i != 0{ + result_string.push_str(","); + result_string.push(c); + continue + } + result_string.push(c) + } + + if negative { + result_string.push_str("-") + } + // Add whitespaces to fit + let mut integer_part_len = result_string.len(); + + let arrange:i16 = CONFIGURATION.fix as i16 - integer_part_len as i16; + + if arrange > 0 { + result_string.push_str(" ".repeat(arrange as usize).as_str()) + } + // -------------------------------------- + // All below is writen because. + // float_number.to_string() printed as 123.321 -> 123.32099999999999795 + let mut reverse_ans = result_string.chars().rev().collect::(); + if negative { + integer_part_len -= 1 + } + if frac_part > 0.0{ + let f_str = format!("{}",ans); + reverse_ans.push_str(&f_str[integer_part_len..]); + } + + println!("{}", reverse_ans) } fn parse_arguments() -> Configuration { -- cgit v1.2.3