Codebase list golang-github-sanity-io-litter / 5286198
Ensure that floats with no fractional digits are always emitted with fractional digits. Fixes #44. Alexander Staubo 2 years ago
3 changed file(s) with 11 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
7171 uint(10),
7272 float32(12.3),
7373 float64(12.3),
74 float32(1.0),
75 float64(1.0),
7476 complex64(12 + 10.5i),
7577 complex128(-1.2 - 0.1i),
7678 (func(v int) *int { return &v })(10),
11
22 import (
33 "io"
4 "math"
45 "strconv"
56 )
67
2122 }
2223
2324 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 }
2531 }
2632
2733 func printComplex(w io.Writer, c complex128, floatPrecision int) {
1212 10,
1313 12.3,
1414 12.3,
15 1.0,
16 1.0,
1517 complex64(12+10.5i),
1618 complex128(-1.2-0.1i),
1719 &10,