Fix float64 truncation error (#293)
Don't truncate float64 representation on marashaling. Fixes https://github.com/pelletier/go-toml/issues/289
This commit is contained in:
@@ -27,6 +27,7 @@ title = "TOML Marshal Testing"
|
|||||||
uint = 5001
|
uint = 5001
|
||||||
bool = true
|
bool = true
|
||||||
float = 123.4
|
float = 123.4
|
||||||
|
float64 = 123.456782132399
|
||||||
int = 5000
|
int = 5000
|
||||||
string = "Bite me"
|
string = "Bite me"
|
||||||
date = 1979-05-27T07:32:00Z
|
date = 1979-05-27T07:32:00Z
|
||||||
|
|||||||
+4
-2
@@ -135,7 +135,8 @@ type testMapDoc struct {
|
|||||||
type testDocBasics struct {
|
type testDocBasics struct {
|
||||||
Uint uint `toml:"uint"`
|
Uint uint `toml:"uint"`
|
||||||
Bool bool `toml:"bool"`
|
Bool bool `toml:"bool"`
|
||||||
Float float32 `toml:"float"`
|
Float32 float32 `toml:"float"`
|
||||||
|
Float64 float64 `toml:"float64"`
|
||||||
Int int `toml:"int"`
|
Int int `toml:"int"`
|
||||||
String *string `toml:"string"`
|
String *string `toml:"string"`
|
||||||
Date time.Time `toml:"date"`
|
Date time.Time `toml:"date"`
|
||||||
@@ -174,7 +175,8 @@ var docData = testDoc{
|
|||||||
Basics: testDocBasics{
|
Basics: testDocBasics{
|
||||||
Bool: true,
|
Bool: true,
|
||||||
Date: time.Date(1979, 5, 27, 7, 32, 0, 0, time.UTC),
|
Date: time.Date(1979, 5, 27, 7, 32, 0, 0, time.UTC),
|
||||||
Float: 123.4,
|
Float32: 123.4,
|
||||||
|
Float64: 123.456782132399,
|
||||||
Int: 5000,
|
Int: 5000,
|
||||||
Uint: 5001,
|
Uint: 5001,
|
||||||
String: &biteMe,
|
String: &biteMe,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ title = "TOML Marshal Testing"
|
|||||||
bool = true
|
bool = true
|
||||||
date = 1979-05-27T07:32:00Z
|
date = 1979-05-27T07:32:00Z
|
||||||
float = 123.4
|
float = 123.4
|
||||||
|
float64 = 123.456782132399
|
||||||
int = 5000
|
int = 5000
|
||||||
string = "Bite me"
|
string = "Bite me"
|
||||||
uint = 5001
|
uint = 5001
|
||||||
|
|||||||
+14
-5
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -106,12 +107,20 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
|
|||||||
case int64:
|
case int64:
|
||||||
return strconv.FormatInt(value, 10), nil
|
return strconv.FormatInt(value, 10), nil
|
||||||
case float64:
|
case float64:
|
||||||
// Ensure a round float does contain a decimal point. Otherwise feeding
|
// Default bit length is full 64
|
||||||
// the output back to the parser would convert to an integer.
|
bits := 64
|
||||||
if math.Trunc(value) == value {
|
// Float panics if nan is used
|
||||||
return strings.ToLower(strconv.FormatFloat(value, 'f', 1, 32)), nil
|
if !math.IsNaN(value) {
|
||||||
|
// if 32 bit accuracy is enough to exactly show, use 32
|
||||||
|
_, acc := big.NewFloat(value).Float32()
|
||||||
|
if acc == big.Exact {
|
||||||
|
bits = 32
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, 32)), nil
|
if math.Trunc(value) == value {
|
||||||
|
return strings.ToLower(strconv.FormatFloat(value, 'f', 1, bits)), nil
|
||||||
|
}
|
||||||
|
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, bits)), nil
|
||||||
case string:
|
case string:
|
||||||
if tv.multiline {
|
if tv.multiline {
|
||||||
return "\"\"\"\n" + encodeMultilineTomlString(value) + "\"\"\"", nil
|
return "\"\"\"\n" + encodeMultilineTomlString(value) + "\"\"\"", nil
|
||||||
|
|||||||
Reference in New Issue
Block a user