File tree Expand file tree Collapse file tree 4 files changed +18
-7
lines changed
builtin_strings_and_interpolation Expand file tree Collapse file tree 4 files changed +18
-7
lines changed Original file line number Diff line number Diff line change @@ -491,13 +491,17 @@ pub fn remove_tail_zeros(s string) string {
491
491
if i_s < s.len && s[i_s] == `.` {
492
492
mut i_s1 := i_s + 1
493
493
mut sum := 0
494
+ mut i_s2 := i_s1 // last non-zero index after `.`
494
495
for i_s1 < s.len && s[i_s1 ] > = `0` && s[i_s1 ] < = `9` {
495
496
sum + = s[i_s1 ] - u8 (`0` )
497
+ if s[i_s1 ] != `0` {
498
+ i_s2 = i_s1
499
+ }
496
500
i_s1 ++
497
501
}
498
502
// decimal part must be copied
499
503
if sum > 0 {
500
- for c_i in i_s .. i_s 1 {
504
+ for c_i in i_s .. i_s 2 + 1 {
501
505
buf[i_d] = s[c_i]
502
506
i_d++
503
507
}
Original file line number Diff line number Diff line change @@ -119,3 +119,10 @@ fn test_sprintf_with_escape() {
119
119
s := unsafe { strconv.v_sprintf ('%d is 100%% awesome' , n) }
120
120
assert s == '69 is 100% awesome'
121
121
}
122
+
123
+ fn test_remove_tail_zeros () {
124
+ assert strconv.remove_tail_zeros ('1.234000000000' ) == '1.234'
125
+ assert strconv.remove_tail_zeros ('1.0000000' ) == '1'
126
+ assert strconv.remove_tail_zeros ('1234' ) == '1234'
127
+ assert strconv.remove_tail_zeros ('1.00000000007' ) == '1.00000000007'
128
+ }
Original file line number Diff line number Diff line change 1
1
fn test_string_interpolation_float_fmt () {
2
2
mut a := 76.295
3
3
eprintln ('${a:8.2} ' )
4
- assert '${a:8.2} ' == ' 76.30 '
4
+ assert '${a:8.2} ' == ' 76.3 '
5
5
eprintln ('${a:8.2f} ' )
6
6
assert '${a:8.2f} ' == ' 76.30'
7
7
8
8
a = 76.296
9
9
eprintln ('${a:8.2} ' )
10
- assert '${a:8.2} ' == ' 76.30 '
10
+ assert '${a:8.2} ' == ' 76.3 '
11
11
eprintln ('${a:8.2f} ' )
12
12
assert '${a:8.2f} ' == ' 76.30'
13
13
}
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ fn test_cast_to_empty_interface() {
22
22
assert ret_strings[1 ] == 'int 22'
23
23
assert ret_strings[2 ] == 'int 8888'
24
24
assert ret_strings[3 ] == 'int 9999'
25
- assert ret_strings[4 ] == 'f64 1.110 '
26
- assert ret_strings[5 ] == 'f64 2.220 '
27
- assert ret_strings[6 ] == 'f64 8.880 '
28
- assert ret_strings[7 ] == 'f64 9.990 '
25
+ assert ret_strings[4 ] == 'f64 1.11 '
26
+ assert ret_strings[5 ] == 'f64 2.22 '
27
+ assert ret_strings[6 ] == 'f64 8.88 '
28
+ assert ret_strings[7 ] == 'f64 9.99 '
29
29
}
You can’t perform that action at this time.
0 commit comments