strconv: fix error for string interpolation of float format (#13800)

pull/13802/head
yuyi 2022-03-22 18:00:18 +08:00 committed by GitHub
parent afbccf79f7
commit 56f5ed4789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -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}") // println("r_i-d_pos: ${r_i - d_pos}")
if dot_res_sp >= 0 { 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 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)}]") // println("result: [${tos(&res[0],r_i)}]")
tmp_res := tos(res.data, r_i).clone() tmp_res := tos(res.data, r_i).clone()
res.free() res.free()

View File

@ -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'
}