Skip to content
Snippets Groups Projects
Commit 1249e92e authored by Edward Welch's avatar Edward Welch Committed by Ed
Browse files

use strconf.FormatFloat instead of fmt.Sprintf for converting floats to...

use strconf.FormatFloat instead of fmt.Sprintf for converting floats to strings, this way we can eliminate non significant trailing zeros such that the float value 1 would be "1" as a string instead of "1.000000"
parent ba0261f9
No related branches found
No related tags found
No related merge requests found
......@@ -85,9 +85,9 @@ func getString(unk interface{}) (string, error) {
switch i := unk.(type) {
case float64:
return fmt.Sprintf("%f", i), nil
return strconv.FormatFloat(i, 'f', -1, 64), nil
case float32:
return fmt.Sprintf("%f", i), nil
return strconv.FormatFloat(float64(i), 'f', -1, 32), nil
case int64:
return strconv.FormatInt(i, 10), nil
case int32:
......
......@@ -37,3 +37,31 @@ func assertLabels(t *testing.T, expect map[string]string, got model.LabelSet) {
assert.Equal(t, model.LabelValue(v), gotV, "mismatch label value")
}
}
// Verify the formatting of float conversion to make sure there are not any trailing zeros,
// and also make sure unix timestamps are converted properly
func TestGetString(t *testing.T) {
var f64, f64_1 float64
var f32 float32
f64 = 1
f64_1 = 1562723913000
f32 = 2.02
s64, err := getString(f64)
if err != nil {
t.Errorf("Failed to get string from float... this shouldn't have happened: %v", err)
return
}
s64_1, err := getString(f64_1)
if err != nil {
t.Errorf("Failed to get string from float... this shouldn't have happened: %v", err)
return
}
s32, err := getString(f32)
if err != nil {
t.Errorf("Failed to get string from float... this shouldn't have happened: %v", err)
return
}
assert.Equal(t, "1", s64)
assert.Equal(t, "2.02", s32)
assert.Equal(t, "1562723913000", s64_1)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment