diff --git a/encode.go b/encode.go index b3c62b39..a14435e8 100644 --- a/encode.go +++ b/encode.go @@ -333,7 +333,13 @@ func (e *encoder) timev(tag string, in reflect.Value) { } func (e *encoder) floatv(tag string, in reflect.Value) { - s := strconv.FormatFloat(in.Float(), 'g', -1, 64) + // Issue #352: When formatting, use the precision of the underlying value + precision := 64 + if in.Kind() == reflect.Float32 { + precision = 32 + } + + s := strconv.FormatFloat(in.Float(), 'g', -1, precision) switch s { case "+Inf": s = ".inf" diff --git a/encode_test.go b/encode_test.go index 0324a909..f0911a76 100644 --- a/encode_test.go +++ b/encode_test.go @@ -75,6 +75,9 @@ var marshalTests = []struct { }, { map[string]interface{}{"v": float64(0.1)}, "v: 0.1\n", + }, { + map[string]interface{}{"v": float32(0.99)}, + "v: 0.99\n", }, { map[string]interface{}{"v": -0.1}, "v: -0.1\n",