From 56f5ed47898af13f27946ec5a592f218ca4ec99b Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 22 Mar 2022 18:00:18 +0800 Subject: [PATCH] strconv: fix error for string interpolation of float format (#13800) --- vlib/strconv/format_mem.c.v | 9 ++++++--- vlib/v/tests/string_interpolation_float_fmt_test.v | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/string_interpolation_float_fmt_test.v diff --git a/vlib/strconv/format_mem.c.v b/vlib/strconv/format_mem.c.v index c2229d534d..5a5da0d93e 100644 --- a/vlib/strconv/format_mem.c.v +++ b/vlib/strconv/format_mem.c.v @@ -274,10 +274,13 @@ pub fn f64_to_str_lnd1(f f64, dec_digit int) string { // println("r_i-d_pos: ${r_i - d_pos}") if dot_res_sp >= 0 { - if (r_i - dot_res_sp) > dec_digit { - r_i = dot_res_sp + dec_digit + 1 - } + r_i = dot_res_sp + dec_digit + 1 res[r_i] = 0 + for c1 in 1 .. dec_digit + 1 { + if res[r_i - c1] == 0 { + res[r_i - c1] = `0` + } + } // println("result: [${tos(&res[0],r_i)}]") tmp_res := tos(res.data, r_i).clone() res.free() diff --git a/vlib/v/tests/string_interpolation_float_fmt_test.v b/vlib/v/tests/string_interpolation_float_fmt_test.v new file mode 100644 index 0000000000..37c4fe960a --- /dev/null +++ b/vlib/v/tests/string_interpolation_float_fmt_test.v @@ -0,0 +1,13 @@ +fn test_string_interpolation_float_fmt() { + mut a := 76.295 + eprintln('${a:8.2}') + assert '${a:8.2}' == ' 76.30' + eprintln('${a:8.2f}') + assert '${a:8.2f}' == ' 76.30' + + a = 76.296 + eprintln('${a:8.2}') + assert '${a:8.2}' == ' 76.30' + eprintln('${a:8.2f}') + assert '${a:8.2f}' == ' 76.30' +}