diff options
author | Vla Mai <[email protected]> | 2019-04-02 02:30:13 +0100 |
---|---|---|
committer | Vla Mai <[email protected]> | 2019-04-02 02:30:13 +0100 |
commit | f4a5fbd1d64e3e013ce8ca5776805ee5fd7a58e8 (patch) | |
tree | 33400be7b4f1c280a10386e88111fa88e649cf81 | |
parent | 4d1e44fd4c6237228c6d366ba371d9c88a9311e1 (diff) |
add thousend separator print
-rw-r--r-- | src/main.rs | 42 |
1 files changed, 41 insertions, 1 deletions
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() { | |||
97 | } | 97 | } |
98 | 98 | ||
99 | fn pprint(ans: f64) { | 99 | fn pprint(ans: f64) { |
100 | println!("{a:width$}", width = CONFIGURATION.fix, a = ans) | 100 | // remove "-" char to add it later |
101 | let negative = ans < 0.0; | ||
102 | let ans = ans.abs(); | ||
103 | |||
104 | let mut result_string = String::new(); | ||
105 | let frac_part = ans.fract(); | ||
106 | let i_part = ans - frac_part; | ||
107 | |||
108 | for (i,c) in i_part.to_string().chars().rev().enumerate(){ | ||
109 | if i % 3 == 0 && i != 0{ | ||
110 | result_string.push_str(","); | ||
111 | result_string.push(c); | ||
112 | continue | ||
113 | } | ||
114 | result_string.push(c) | ||
115 | } | ||
116 | |||
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 | |||
125 | if arrange > 0 { | ||
126 | result_string.push_str(" ".repeat(arrange as usize).as_str()) | ||
127 | } | ||
128 | // -------------------------------------- | ||
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) | ||
101 | } | 141 | } |
102 | 142 | ||
103 | fn parse_arguments() -> Configuration { | 143 | fn parse_arguments() -> Configuration { |