@@ -137,6 +137,15 @@ pub fn format_exponent(precision: usize, magnitude: f64, case: Case) -> String {
137
137
}
138
138
}
139
139
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
+
140
149
pub fn format_general ( precision : usize , magnitude : f64 , case : Case ) -> String {
141
150
match magnitude {
142
151
magnitude if magnitude. is_finite ( ) => {
@@ -154,15 +163,12 @@ pub fn format_general(precision: usize, magnitude: f64, case: Case) -> String {
154
163
Case :: Upper => 'E' ,
155
164
} ;
156
165
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) ) ;
161
167
format ! ( "{}{}{:+#03}" , base, e, exponent)
162
168
} else {
163
169
let precision = ( precision as i64 ) - 1 - exponent;
164
170
let precision = precision as usize ;
165
- format ! ( "{:.*}" , precision, magnitude)
171
+ remove_trailing_zeros ( format ! ( "{:.*}" , precision, magnitude) )
166
172
}
167
173
}
168
174
magnitude if magnitude. is_nan ( ) => format_nan ( case) ,
0 commit comments