Use scientific notation when number_to_text rounds up to 1e15

Found while testing crates/core/src/value.rs:354-367

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-10-20 18:16:31 +00:00
co-authored by Claude Haiku 5.5
parent 68e58a2eae
commit f29ac7d9ef
+11
View File
@@ -359,6 +359,10 @@ pub fn number_to_text(n: f64) -> String {
if !(1e-9..1e15).contains(&a) && a.round() != a || a >= 1e15 {
return sci_text(n, 15);
}
// A value just below 1e15 can round up to 1e15 at 15 significant digits.
if format!("{:.14e}", a).parse::<f64>().is_ok_and(|r| r >= 1e15) {
return sci_text(n, 15);
}
// 15 significant digits, trailing zeros trimmed.
let mag = a.log10().floor() as i32;
let decimals = (14 - mag).clamp(0, 30) as usize;
@@ -402,6 +406,13 @@ mod tests {
assert_eq!(number_to_text(1.0 / 3.0), "0.333333333333333");
}
#[test]
fn number_text_rounds_up_to_scientific_boundary() {
assert_eq!(number_to_text(999999999999999.9), "1E+15");
assert_eq!(number_to_text(-999999999999999.9), "-1E+15");
assert_eq!(number_to_text(999999999999999.0), "999999999999999");
}
#[test]
fn coercion() {
assert_eq!(Value::from("12").to_number(), Ok(12.0));