(
precision: usize,
magnitude: f64,
case: Case,
alternate_form: bool,
always_shows_fract: bool,
)
| 124 | } |
| 125 | |
| 126 | pub fn format_general( |
| 127 | precision: usize, |
| 128 | magnitude: f64, |
| 129 | case: Case, |
| 130 | alternate_form: bool, |
| 131 | always_shows_fract: bool, |
| 132 | ) -> String { |
| 133 | match magnitude { |
| 134 | magnitude if magnitude.is_finite() => { |
| 135 | let r_exp = format!("{:.*e}", precision.saturating_sub(1), magnitude); |
| 136 | let mut parts = r_exp.splitn(2, 'e'); |
| 137 | let base = parts.next().unwrap(); |
| 138 | let exponent = parts.next().unwrap().parse::<i64>().unwrap(); |
| 139 | if exponent < -4 || exponent + (always_shows_fract as i64) >= (precision as i64) { |
| 140 | let e = match case { |
| 141 | Case::Lower => 'e', |
| 142 | Case::Upper => 'E', |
| 143 | }; |
| 144 | let magnitude = format!("{:.*}", precision + 1, base); |
| 145 | let base = maybe_remove_trailing_redundant_chars(magnitude, alternate_form); |
| 146 | let point = decimal_point_or_empty(precision.saturating_sub(1), alternate_form); |
| 147 | format!("{base}{point}{e}{exponent:+#03}") |
| 148 | } else { |
| 149 | let precision = ((precision as i64) - 1 - exponent) as usize; |
| 150 | let magnitude = format!("{magnitude:.precision$}"); |
| 151 | let base = maybe_remove_trailing_redundant_chars(magnitude, alternate_form); |
| 152 | let point = decimal_point_or_empty(precision, alternate_form); |
| 153 | format!("{base}{point}") |
| 154 | } |
| 155 | } |
| 156 | magnitude if magnitude.is_nan() => format_nan(case), |
| 157 | magnitude if magnitude.is_infinite() => format_inf(case), |
| 158 | _ => "".to_string(), |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // TODO: rewrite using format_general |
| 163 | pub fn to_string(value: f64) -> String { |
no test coverage detected