diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 5219c6d..4f5b4fa 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -49,7 +49,7 @@ fn main() { | |||
49 | if CONFIGURATION.input.len() > 0 { | 49 | if CONFIGURATION.input.len() > 0 { |
50 | let evaled = eval_math_expression(&CONFIGURATION.input[..]); | 50 | let evaled = eval_math_expression(&CONFIGURATION.input[..]); |
51 | match evaled { | 51 | match evaled { |
52 | Ok(ans) => println!("{:.*}", CONFIGURATION.fix, ans), | 52 | Ok(ans) => pprint(ans), |
53 | Err(e) => { | 53 | Err(e) => { |
54 | eprintln!("{}", handler(e)); | 54 | eprintln!("{}", handler(e)); |
55 | std::process::exit(1); | 55 | std::process::exit(1); |
@@ -74,7 +74,7 @@ fn main() { | |||
74 | rl.add_history_entry(line.as_ref()); | 74 | rl.add_history_entry(line.as_ref()); |
75 | let evaled = eval_math_expression(&line[..]); | 75 | let evaled = eval_math_expression(&line[..]); |
76 | match evaled { | 76 | match evaled { |
77 | Ok(ans) => println!("{:.*}", CONFIGURATION.fix, ans), | 77 | Ok(ans) => pprint(ans), |
78 | Err(e) => println!("{}", handler(e)), | 78 | Err(e) => println!("{}", handler(e)), |
79 | }; | 79 | }; |
80 | }, | 80 | }, |
@@ -96,6 +96,33 @@ fn main() { | |||
96 | } | 96 | } |
97 | } | 97 | } |
98 | 98 | ||
99 | fn pprint(ans: f64) { | ||
100 | let ans_string = format!("{}",ans); | ||
101 | |||
102 | let ans_vector: Vec<&str> = ans_string.split(".").collect(); | ||
103 | match ans_vector.len() { | ||
104 | 1 => println!("{}",thousand_sep(ans_vector[0])), | ||
105 | 2 => println!("{}.{}",thousand_sep(ans_vector[0]),ans_vector[1]), | ||
106 | _ => () | ||
107 | } | ||
108 | } | ||
109 | |||
110 | fn thousand_sep(inp:&str) -> String{ | ||
111 | let mut result_string = String::new(); | ||
112 | for (i,c) in inp.to_string().chars().rev().enumerate(){ | ||
113 | if i % 3 == 0 && i != 0 && c.to_string() != "-"{ | ||
114 | result_string.push_str(","); | ||
115 | } | ||
116 | result_string.push(c) | ||
117 | } | ||
118 | let arrange:i16 = CONFIGURATION.fix as i16 - result_string.len() as i16; | ||
119 | |||
120 | if arrange > 0 { | ||
121 | result_string.push_str(" ".repeat(arrange as usize).as_str()) | ||
122 | } | ||
123 | result_string.chars().rev().collect::<String>() | ||
124 | } | ||
125 | |||
99 | fn parse_arguments() -> Configuration { | 126 | fn parse_arguments() -> Configuration { |
100 | let config = App::new(env!("CARGO_PKG_NAME")) | 127 | let config = App::new(env!("CARGO_PKG_NAME")) |
101 | .version(env!("CARGO_PKG_VERSION")) | 128 | .version(env!("CARGO_PKG_VERSION")) |
@@ -119,7 +146,7 @@ fn parse_arguments() -> Configuration { | |||
119 | let mut input = String::new(); | 146 | let mut input = String::new(); |
120 | if let Some(i) = config.value_of("INPUT") { | 147 | if let Some(i) = config.value_of("INPUT") { |
121 | input.push_str(i); | 148 | input.push_str(i); |
122 | }; | 149 | }; |
123 | Configuration { | 150 | Configuration { |
124 | radian_mode: config.is_present("radian"), | 151 | radian_mode: config.is_present("radian"), |
125 | fix: config.value_of("fix") | 152 | fix: config.value_of("fix") |
@@ -166,7 +193,7 @@ fn eval_math_expression(input: &str) -> Result<f64, CalcError> { | |||
166 | Ok(evaled) | 193 | Ok(evaled) |
167 | } | 194 | } |
168 | 195 | ||
169 | #[cfg(test)] | 196 | #[cfg(test)] |
170 | mod tests { | 197 | mod tests { |
171 | use super::*; | 198 | use super::*; |
172 | 199 | ||