From 4d1e44fd4c6237228c6d366ba371d9c88a9311e1 Mon Sep 17 00:00:00 2001 From: Vla Mai Date: Tue, 2 Apr 2019 03:37:15 +0500 Subject: add print function --- src/main.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 5219c6d..94cbc1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,7 @@ fn main() { if CONFIGURATION.input.len() > 0 { let evaled = eval_math_expression(&CONFIGURATION.input[..]); match evaled { - Ok(ans) => println!("{:.*}", CONFIGURATION.fix, ans), + Ok(ans) => pprint(ans), Err(e) => { eprintln!("{}", handler(e)); std::process::exit(1); @@ -74,7 +74,7 @@ fn main() { rl.add_history_entry(line.as_ref()); let evaled = eval_math_expression(&line[..]); match evaled { - Ok(ans) => println!("{:.*}", CONFIGURATION.fix, ans), + Ok(ans) => pprint(ans), Err(e) => println!("{}", handler(e)), }; }, @@ -96,6 +96,10 @@ fn main() { } } +fn pprint(ans: f64) { + println!("{a:width$}", width = CONFIGURATION.fix, a = ans) +} + fn parse_arguments() -> Configuration { let config = App::new(env!("CARGO_PKG_NAME")) .version(env!("CARGO_PKG_VERSION")) @@ -119,7 +123,7 @@ fn parse_arguments() -> Configuration { let mut input = String::new(); if let Some(i) = config.value_of("INPUT") { input.push_str(i); - }; + }; Configuration { radian_mode: config.is_present("radian"), fix: config.value_of("fix") @@ -166,7 +170,7 @@ fn eval_math_expression(input: &str) -> Result { Ok(evaled) } -#[cfg(test)] +#[cfg(test)] mod tests { use super::*; -- cgit v1.2.3 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(-) (limited to 'src') 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 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') 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 From c1a4b1788b3dc539c580394fe06b9699e8868a47 Mon Sep 17 00:00:00 2001 From: Vla Mai Date: Tue, 2 Apr 2019 10:33:00 +0500 Subject: fix negative number print --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 3816377..43f23ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,14 +110,14 @@ fn pprint(ans: f64) { 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{ + if i % 3 == 0 && i != 0 && c.to_string() != "-"{ result_string.push_str(","); result_string.push(c); continue } result_string.push(c) } - let arrange:i16 = CONFIGURATION.fix as i16 - inp.len() as i16; + let arrange:i16 = CONFIGURATION.fix as i16 - result_string.len() as i16; if arrange > 0 { result_string.push_str(" ".repeat(arrange as usize).as_str()) -- cgit v1.2.3 From b8726a12fd97d04544cb3fa98e7c3f55466a707e Mon Sep 17 00:00:00 2001 From: Vla Mai Date: Tue, 2 Apr 2019 10:57:36 +0500 Subject: small refactor --- src/main.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 43f23ef..4f5b4fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,8 +112,6 @@ fn thousand_sep(inp:&str) -> String{ for (i,c) in inp.to_string().chars().rev().enumerate(){ if i % 3 == 0 && i != 0 && c.to_string() != "-"{ result_string.push_str(","); - result_string.push(c); - continue } result_string.push(c) } -- cgit v1.2.3