Skip to content

Commit

Permalink
use json for writing floats as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Mar 1, 2022
1 parent 4493a5e commit adb62e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion model/pdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ func (a AttributeValue) AsString() string {
return strconv.FormatBool(a.BoolVal())

case AttributeValueTypeDouble:
return strconv.FormatFloat(a.DoubleVal(), 'f', -1, 64)
jsonStr, _ := json.Marshal(a.DoubleVal())
return string(jsonStr)

case AttributeValueTypeInt:
return strconv.FormatInt(a.IntVal(), 10)
Expand Down
5 changes: 5 additions & 0 deletions model/pdata/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,11 @@ func TestAsString(t *testing.T) {
input: NewAttributeValueDouble(1.61803399),
expected: "1.61803399",
},
{
name: "small float64",
input: NewAttributeValueDouble(.000000009),
expected: "9e-9",
},
{
name: "boolean",
input: NewAttributeValueBool(true),
Expand Down

0 comments on commit adb62e2

Please sign in to comment.