Formats floats into Python style exponent notation, by first formatting in Rust style exponent notation (`1.0000e0`), then convert to Python style (`1.0000e+00`).
(
precision: usize,
magnitude: f64,
case: Case,
alternate_form: bool,
)
| 70 | // Formats floats into Python style exponent notation, by first formatting in Rust style |
| 71 | // exponent notation (`1.0000e0`), then convert to Python style (`1.0000e+00`). |
| 72 | pub fn format_exponent( |
| 73 | precision: usize, |
| 74 | magnitude: f64, |
| 75 | case: Case, |
| 76 | alternate_form: bool, |
| 77 | ) -> String { |
| 78 | match magnitude { |
| 79 | magnitude if magnitude.is_finite() => { |
| 80 | let r_exp = format!("{magnitude:.precision$e}"); |
| 81 | let mut parts = r_exp.splitn(2, 'e'); |
| 82 | let base = parts.next().unwrap(); |
| 83 | let exponent = parts.next().unwrap().parse::<i64>().unwrap(); |
| 84 | let e = match case { |
| 85 | Case::Lower => 'e', |
| 86 | Case::Upper => 'E', |
| 87 | }; |
| 88 | let point = decimal_point_or_empty(precision, alternate_form); |
| 89 | format!("{base}{point}{e}{exponent:+#03}") |
| 90 | } |
| 91 | magnitude if magnitude.is_nan() => format_nan(case), |
| 92 | magnitude if magnitude.is_infinite() => format_inf(case), |
| 93 | _ => "".to_string(), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// If s represents a floating point value, trailing zeros and a possibly trailing |
| 98 | /// decimal point will be removed. |
no test coverage detected