Skip to content

Commit

Permalink
Merge pull request #11 from tukaelu/fix/float-to-string
Browse files Browse the repository at this point in the history
fix: the issue of numbers being converted to exponential notation.
  • Loading branch information
tukaelu authored Oct 17, 2023
2 parents 0e87ae9 + 41430c0 commit 8cc901a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 5 additions & 5 deletions internal/exporter/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ func (c *CsvRecord) ToStringArray(friendly bool) []string {
time.Unix(c.Time, 0).Local().String(),
c.Name,
strconv.FormatInt(c.Time, 10),
c.toString(c.Value),
toString(c.Value),
}
} else {
return []string{
c.Name,
strconv.FormatInt(c.Time, 10),
c.toString(c.Value),
toString(c.Value),
}
}
}

func (c *CsvRecord) toString(v any) string {
func toString(v any) string {
switch v := v.(type) {
case string:
return v
case int:
case int, int64:
return fmt.Sprintf("%d", v)
case float64:
return fmt.Sprintf("%g", v)
return strconv.FormatFloat(v, 'f', -1, 64)
case []byte:
return *(*string)(unsafe.Pointer(&v))
default:
Expand Down
13 changes: 13 additions & 0 deletions internal/exporter/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ func TestToStringArray(t *testing.T) {
assert.Equal(t, "1696113420", friendly[2], "Time of arrayed elements must match.")
assert.Equal(t, "0", friendly[3], "Value of arrayed elements must match.")
}

func TestToStringt(t *testing.T) {
namePtn := "cpu.idle.percentage"
timePtn := int64(1696086000)
valuePtn1 := interface{}(1316306944)
valuePtn2 := interface{}(23569.216666666667)
valuePtn3 := interface{}(5.0999080344452805)
assert.Equal(t, namePtn, toString(namePtn), "convert namePtn to string")
assert.Equal(t, "1696086000", toString(timePtn), "convert timePtn to string")
assert.Equal(t, "1316306944", toString(valuePtn1), "convert valuePtn1 to string (Not to be in exponential notation)")
assert.Equal(t, "23569.216666666667", toString(valuePtn2), "convert valuePtn2 to string (Not to be in exponential notation)")
assert.Equal(t, "5.0999080344452805", toString(valuePtn3), "convert valuePtn3 to string (Not to be in exponential notation)")
}

0 comments on commit 8cc901a

Please sign in to comment.