Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #289: Float64 trucating #292

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,9 @@ func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface
return mval.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return mval.Uint(), nil
case reflect.Float32, reflect.Float64:
case reflect.Float32:
return strconv.ParseFloat(strconv.FormatFloat(float64(mval.Interface().(float32)), 'f', -1, 32), 64)
case reflect.Float64:
return mval.Float(), nil
case reflect.String:
return mval.String(), nil
Expand Down
21 changes: 20 additions & 1 deletion marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Ystrlist = ["Howdy","Hey There"]
String2 = "Three"
`)

var testMarshalFloat64 = []byte(`Value = 1.24675324675
`)

func TestBasicMarshal(t *testing.T) {
result, err := Marshal(basicTestData)
if err != nil {
Expand Down Expand Up @@ -101,6 +104,22 @@ func TestBasicMarshalOrderedWithPointer(t *testing.T) {
}
}

func TestMarshalFloat64(t *testing.T) {
foo := struct {
Value float64
}{
Value: 1.24675324675,
}
b, err := Marshal(&foo)
if err != nil {
t.Fatal(err)
}
expected := testMarshalFloat64
if !bytes.Equal(b, expected) {
t.Errorf("Bad marshal: expected\n-----\n%s\n-----\ngot\n-----\n%s\n-----\n", expected, b)
}
}

func TestBasicUnmarshal(t *testing.T) {
result := basicMarshalTestStruct{}
err := Unmarshal(basicTestToml, &result)
Expand Down Expand Up @@ -1495,7 +1514,7 @@ func TestUnmarshalPreservesUnexportedFields(t *testing.T) {

[[slice1]]
exported1 = "visible3"

[[slice1]]
exported1 = "visible4"

Expand Down
4 changes: 2 additions & 2 deletions tomltree_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ func tomlValueStringRepresentation(v interface{}, indent string, arraysOneElemen
// 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 strings.ToLower(strconv.FormatFloat(value, 'f', 1, 32)), nil
return strings.ToLower(strconv.FormatFloat(value, 'f', 1, 64)), nil
}
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, 32)), nil
return strings.ToLower(strconv.FormatFloat(value, 'f', -1, 64)), nil
case string:
if tv.multiline {
return "\"\"\"\n" + encodeMultilineTomlString(value) + "\"\"\"", nil
Expand Down