diff options
author | Vla Mai <[email protected]> | 2019-04-02 06:11:45 +0100 |
---|---|---|
committer | Vla Mai <[email protected]> | 2019-04-02 06:11:45 +0100 |
commit | 573e30c62deae1946f9d8c62c6e23a68d6208847 (patch) | |
tree | 85b59ed7c363682e43b534c822d29fe892c436fc | |
parent | f4a5fbd1d64e3e013ce8ca5776805ee5fd7a58e8 (diff) |
refactor
-rw-r--r-- | src/main.rs | 41 |
1 files changed, 13 insertions, 28 deletions
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() { | |||
97 | } | 97 | } |
98 | 98 | ||
99 | fn pprint(ans: f64) { | 99 | fn pprint(ans: f64) { |
100 | // remove "-" char to add it later | 100 | let ans_string = format!("{}",ans); |
101 | let negative = ans < 0.0; | ||
102 | let ans = ans.abs(); | ||
103 | 101 | ||
104 | let mut result_string = String::new(); | 102 | let ans_vector: Vec<&str> = ans_string.split(".").collect(); |
105 | let frac_part = ans.fract(); | 103 | match ans_vector.len() { |
106 | let i_part = ans - frac_part; | 104 | 1 => println!("{}",thousand_sep(ans_vector[0])), |
105 | 2 => println!("{}.{}",thousand_sep(ans_vector[0]),ans_vector[1]), | ||
106 | _ => () | ||
107 | } | ||
108 | } | ||
107 | 109 | ||
108 | for (i,c) in i_part.to_string().chars().rev().enumerate(){ | 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(){ | ||
109 | if i % 3 == 0 && i != 0{ | 113 | if i % 3 == 0 && i != 0{ |
110 | result_string.push_str(","); | 114 | result_string.push_str(","); |
111 | result_string.push(c); | 115 | result_string.push(c); |
@@ -113,31 +117,12 @@ fn pprint(ans: f64) { | |||
113 | } | 117 | } |
114 | result_string.push(c) | 118 | result_string.push(c) |
115 | } | 119 | } |
116 | 120 | let arrange:i16 = CONFIGURATION.fix as i16 - inp.len() as i16; | |
117 | if negative { | ||
118 | result_string.push_str("-") | ||
119 | } | ||
120 | // Add whitespaces to fit | ||
121 | let mut integer_part_len = result_string.len(); | ||
122 | |||
123 | let arrange:i16 = CONFIGURATION.fix as i16 - integer_part_len as i16; | ||
124 | 121 | ||
125 | if arrange > 0 { | 122 | if arrange > 0 { |
126 | result_string.push_str(" ".repeat(arrange as usize).as_str()) | 123 | result_string.push_str(" ".repeat(arrange as usize).as_str()) |
127 | } | 124 | } |
128 | // -------------------------------------- | 125 | result_string.chars().rev().collect::<String>() |
129 | // All below is writen because. | ||
130 | // float_number.to_string() printed as 123.321 -> 123.32099999999999795 | ||
131 | let mut reverse_ans = result_string.chars().rev().collect::<String>(); | ||
132 | if negative { | ||
133 | integer_part_len -= 1 | ||
134 | } | ||
135 | if frac_part > 0.0{ | ||
136 | let f_str = format!("{}",ans); | ||
137 | reverse_ans.push_str(&f_str[integer_part_len..]); | ||
138 | } | ||
139 | |||
140 | println!("{}", reverse_ans) | ||
141 | } | 126 | } |
142 | 127 | ||
143 | fn parse_arguments() -> Configuration { | 128 | fn parse_arguments() -> Configuration { |