Skip to content

Commit f124ec8

Browse files
committed
Fix more trailing zeros in general formatting of floating numbers
1 parent 6da4d97 commit f124ec8

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

common/src/float_ops.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ pub fn format_exponent(precision: usize, magnitude: f64, case: Case) -> String {
137137
}
138138
}
139139

140+
fn remove_trailing_zeros(s: String) -> String {
141+
let mut s = s;
142+
while s.ends_with('0') || s.ends_with('.') {
143+
s.truncate(s.len() - 1);
144+
}
145+
146+
s
147+
}
148+
140149
pub fn format_general(precision: usize, magnitude: f64, case: Case) -> String {
141150
match magnitude {
142151
magnitude if magnitude.is_finite() => {
@@ -154,15 +163,12 @@ pub fn format_general(precision: usize, magnitude: f64, case: Case) -> String {
154163
Case::Upper => 'E',
155164
};
156165

157-
let mut base = format!("{:.*}", precision + 1, base);
158-
while base.ends_with('0') || base.ends_with('.') {
159-
base.truncate(base.len() - 1);
160-
}
166+
let base = remove_trailing_zeros(format!("{:.*}", precision + 1, base));
161167
format!("{}{}{:+#03}", base, e, exponent)
162168
} else {
163169
let precision = (precision as i64) - 1 - exponent;
164170
let precision = precision as usize;
165-
format!("{:.*}", precision, magnitude)
171+
remove_trailing_zeros(format!("{:.*}", precision, magnitude))
166172
}
167173
}
168174
magnitude if magnitude.is_nan() => format_nan(case),

0 commit comments

Comments
 (0)