Ensure that floats with no fractional digits are always emitted with
fractional digits. Fixes #44.
Alexander Staubo
2 years ago
71 | 71 |
uint(10),
|
72 | 72 |
float32(12.3),
|
73 | 73 |
float64(12.3),
|
|
74 |
float32(1.0),
|
|
75 |
float64(1.0),
|
74 | 76 |
complex64(12 + 10.5i),
|
75 | 77 |
complex128(-1.2 - 0.1i),
|
76 | 78 |
(func(v int) *int { return &v })(10),
|
1 | 1 |
|
2 | 2 |
import (
|
3 | 3 |
"io"
|
|
4 |
"math"
|
4 | 5 |
"strconv"
|
5 | 6 |
)
|
6 | 7 |
|
|
21 | 22 |
}
|
22 | 23 |
|
23 | 24 |
func printFloat(w io.Writer, val float64, precision int) {
|
24 | |
w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision)))
|
|
25 |
if math.Trunc(val) == val {
|
|
26 |
// Ensure that floats like 1.0 are always printed with a decimal point
|
|
27 |
w.Write([]byte(strconv.FormatFloat(val, 'f', 1, precision)))
|
|
28 |
} else {
|
|
29 |
w.Write([]byte(strconv.FormatFloat(val, 'g', -1, precision)))
|
|
30 |
}
|
25 | 31 |
}
|
26 | 32 |
|
27 | 33 |
func printComplex(w io.Writer, c complex128, floatPrecision int) {
|