Fix toString float encoding (#172)

Ensure a round float does contain a decimal point. Otherwise
feeding the output back to the parser would convert to an integer.

Fixes #171
This commit is contained in:
Thomas Pelletier
2017-06-01 21:36:58 -07:00
committed by GitHub
parent a60c71373e
commit 0d5a6db8dd
2 changed files with 21 additions and 0 deletions
+6
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"math"
"reflect"
"sort"
"strconv"
@@ -50,6 +51,11 @@ func tomlValueStringRepresentation(v interface{}) (string, error) {
case int64:
return strconv.FormatInt(value, 10), nil
case float64:
// Ensure a round float does contain a decimal point. Otherwise feeding
// the output back to the parser would convert to an integer.
if math.Trunc(value) == value {
return strconv.FormatFloat(value, 'f', 1, 32), nil
}
return strconv.FormatFloat(value, 'f', -1, 32), nil
case string:
return "\"" + encodeTomlString(value) + "\"", nil