From ef88570a444479611a4157f1c2e3ffb75444f090 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 25 Mar 2026 22:50:10 +0300 Subject: [PATCH] strconv: fix string interpolation with large float format (fixes #23359) --- vlib/strconv/format_mem.c.v | 8 +++++--- .../string_interpolation_floats_test.v | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/vlib/strconv/format_mem.c.v b/vlib/strconv/format_mem.c.v index b8037713f..9508c2551 100644 --- a/vlib/strconv/format_mem.c.v +++ b/vlib/strconv/format_mem.c.v @@ -207,9 +207,11 @@ pub fn f64_to_str_lnd1(f f64, dec_digit int) string { c++ } - // allocate exp+32 chars for the return string - // mut res := []u8{len:exp+32,init:`0`} - mut res := []u8{len: exp + 40, init: 0} + // Reserve enough space for the current digits, exponent-driven zeros, + // requested fractional padding, and the trailing NUL. + extra_frac_digits := if dec_digit > 0 { dec_digit } else { 0 } + sign_len := if sgn < 0 { 1 } else { 0 } + mut res := []u8{len: sign_len + i1 + exp + extra_frac_digits + 4, init: 0} mut r_i := 0 // result string buffer index // println("s:${sgn} b:${b[0]} es:${exp_sgn} exp:${exp}") diff --git a/vlib/v/tests/builtin_strings_and_interpolation/string_interpolation_floats_test.v b/vlib/v/tests/builtin_strings_and_interpolation/string_interpolation_floats_test.v index ecbcfb3cf..1165a8f79 100644 --- a/vlib/v/tests/builtin_strings_and_interpolation/string_interpolation_floats_test.v +++ b/vlib/v/tests/builtin_strings_and_interpolation/string_interpolation_floats_test.v @@ -27,3 +27,8 @@ fn test_f64_widths_and_precision() { y := f64(200.90) assert '|${y:.0f}|' == '|201|' } + +fn test_f64_widths_and_large_precision_for_small_negative_value() { + x := -0.68849533748148505907238359213806688785552978515625 + assert '|${x:54.40f}|' == '| -0.6884953374814851000000000000000000000000|' +} -- 2.39.5