diff --git a/crates/core/src/value.rs b/crates/core/src/value.rs index cfef7ac57..9a5c09bdf 100644 --- a/crates/core/src/value.rs +++ b/crates/core/src/value.rs @@ -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::().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));