Better sample value string formatting.

This forces a float format without exponent that uses the minimum number
of digits necessary to display a given sample value.
This commit is contained in:
Julius Volz 2015-01-25 23:52:11 +01:00
parent 316b327693
commit 4a842c5da0
2 changed files with 6 additions and 4 deletions

View File

@ -15,6 +15,7 @@ package model
import ( import (
"fmt" "fmt"
"strconv"
) )
// A SampleValue is a representation of a value for a given sample at a given // A SampleValue is a representation of a value for a given sample at a given
@ -28,9 +29,9 @@ func (v SampleValue) Equal(o SampleValue) bool {
// MarshalJSON implements json.Marshaler. // MarshalJSON implements json.Marshaler.
func (v SampleValue) MarshalJSON() ([]byte, error) { func (v SampleValue) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%f"`, v)), nil return []byte(fmt.Sprintf(`"%s"`, v)), nil
} }
func (v SampleValue) String() string { func (v SampleValue) String() string {
return fmt.Sprint(float64(v)) return strconv.FormatFloat(float64(v), 'f', -1, 64)
} }

View File

@ -14,7 +14,8 @@
package model package model
import ( import (
"fmt" "strconv"
native_time "time" native_time "time"
) )
@ -81,7 +82,7 @@ func (t Timestamp) UnixNano() int64 {
// String returns a string representation of the timestamp. // String returns a string representation of the timestamp.
func (t Timestamp) String() string { func (t Timestamp) String() string {
return fmt.Sprintf("%f", float64(t)/float64(second)) return strconv.FormatFloat(float64(t)/float64(second), 'f', -1, 64)
} }
func (t Timestamp) MarshalJSON() ([]byte, error) { func (t Timestamp) MarshalJSON() ([]byte, error) {