From 573e30c62deae1946f9d8c62c6e23a68d6208847 Mon Sep 17 00:00:00 2001 From: Vla Mai Date: Tue, 2 Apr 2019 10:11:45 +0500 Subject: refactor --- src/main.rs | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 3b8ab6d..3816377 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,15 +97,19 @@ fn main() { } fn pprint(ans: f64) { - // remove "-" char to add it later - let negative = ans < 0.0; - let ans = ans.abs(); + let ans_string = format!("{}",ans); - let mut result_string = String::new(); - let frac_part = ans.fract(); - let i_part = ans - frac_part; + let ans_vector: Vec<&str> = ans_string.split(".").collect(); + match ans_vector.len() { + 1 => println!("{}",thousand_sep(ans_vector[0])), + 2 => println!("{}.{}",thousand_sep(ans_vector[0]),ans_vector[1]), + _ => () + } +} - for (i,c) in i_part.to_string().chars().rev().enumerate(){ +fn thousand_sep(inp:&str) -> String{ + let mut result_string = String::new(); + for (i,c) in inp.to_string().chars().rev().enumerate(){ if i % 3 == 0 && i != 0{ result_string.push_str(","); result_string.push(c); @@ -113,31 +117,12 @@ fn pprint(ans: f64) { } 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; + let arrange:i16 = CONFIGURATION.fix as i16 - inp.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) + result_string.chars().rev().collect::() } fn parse_arguments() -> Configuration { -- cgit v1.2.3