diff --git a/marshal.go b/marshal.go index 6984dd8f..4f0f8cf5 100644 --- a/marshal.go +++ b/marshal.go @@ -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 diff --git a/marshal_test.go b/marshal_test.go index 45c0e921..ecd0fa34 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -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 { @@ -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) @@ -1495,7 +1514,7 @@ func TestUnmarshalPreservesUnexportedFields(t *testing.T) { [[slice1]] exported1 = "visible3" - + [[slice1]] exported1 = "visible4" diff --git a/tomltree_write.go b/tomltree_write.go index 9381a88f..93acb5d4 100644 --- a/tomltree_write.go +++ b/tomltree_write.go @@ -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