Skip to content

Commit a0c87c4

Browse files
authored
Merge pull request #995 from judehung/989-odesa
fix: parse float to string with precision to a sufficient level
2 parents 7a8d92e + 48f27d9 commit a0c87c4

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

dtos/event_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestEvent_AddSimpleReading(t *testing.T) {
147147
value string
148148
}{
149149
{int32(12345), "myInt32", common.ValueTypeInt32, "12345"},
150-
{float32(12345.4567), "myFloat32", common.ValueTypeFloat32, "1.234546e+04"},
150+
{float32(12345.4567), "myFloat32", common.ValueTypeFloat32, "1.2345457e+04"},
151151
{[]bool{false, true, false}, "myBoolArray", common.ValueTypeBoolArray, "[false, true, false]"},
152152
}
153153
expectedReadingsCount := len(expectedReadingDetails)

dtos/reading.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,24 @@ func convertFloatValue(valueType string, kind reflect.Kind, value any) (string,
203203
return "", err
204204
}
205205

206-
return fmt.Sprintf("%e", value), nil
206+
switch kind {
207+
case reflect.Float32:
208+
// as above has validated the value type/kind/value, it is safe to cast the value to float32 here
209+
float32Val, ok := value.(float32)
210+
if !ok {
211+
return "", fmt.Errorf("unable to cast value to float32 for %s", valueType)
212+
}
213+
return strconv.FormatFloat(float64(float32Val), 'e', -1, 32), nil
214+
case reflect.Float64:
215+
// as above has validated the value type/kind/value, it is safe to cast the value to float64 here
216+
float64Val, ok := value.(float64)
217+
if !ok {
218+
return "", fmt.Errorf("unable to cast value to float64 for %s", valueType)
219+
}
220+
return strconv.FormatFloat(float64Val, 'e', -1, 64), nil
221+
default:
222+
return "", fmt.Errorf("invalid kind %s to convert float value to string", kind.String())
223+
}
207224
}
208225

209226
func convertSimpleArrayValue(valueType string, kind reflect.Kind, value any) (string, error) {

dtos/reading_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ func TestNewSimpleReading(t *testing.T) {
124124
{"Simple int16", common.ValueTypeInt16, int16(-12345), "-12345"},
125125
{"Simple int32", common.ValueTypeInt32, int32(-1234567890), "-1234567890"},
126126
{"Simple int64", common.ValueTypeInt64, int64(-1234567890987654321), "-1234567890987654321"},
127-
{"Simple Float32", common.ValueTypeFloat32, float32(123.456), "1.234560e+02"},
128-
{"Simple Float64", common.ValueTypeFloat64, float64(123456789.0987654321), "1.234568e+08"},
127+
{"Simple Float32", common.ValueTypeFloat32, float32(123.456), "1.23456e+02"},
128+
{"Simple Float64", common.ValueTypeFloat64, float64(123456789.0987654321), "1.2345678909876543e+08"},
129129
{"Simple Boolean Array", common.ValueTypeBoolArray, []bool{true, false}, "[true, false]"},
130130
{"Simple String Array", common.ValueTypeStringArray, []string{"hello", "world"}, "[hello, world]"},
131131
{"Simple Uint8 Array", common.ValueTypeUint8Array, []uint8{123, 21}, "[123, 21]"},
@@ -136,8 +136,8 @@ func TestNewSimpleReading(t *testing.T) {
136136
{"Simple Int16 Array", common.ValueTypeInt16Array, []int16{-12345, 12345}, "[-12345, 12345]"},
137137
{"Simple Int32 Array", common.ValueTypeInt32Array, []int32{-1234567890, 1234567890}, "[-1234567890, 1234567890]"},
138138
{"Simple Int64 Array", common.ValueTypeInt64Array, []int64{-1234567890987654321, 1234567890987654321}, "[-1234567890987654321, 1234567890987654321]"},
139-
{"Simple Float32 Array", common.ValueTypeFloat32Array, []float32{123.456, -654.321}, "[1.234560e+02, -6.543210e+02]"},
140-
{"Simple Float64 Array", common.ValueTypeFloat64Array, []float64{123456789.0987654321, -987654321.123456789}, "[1.234568e+08, -9.876543e+08]"},
139+
{"Simple Float32 Array", common.ValueTypeFloat32Array, []float32{123.456, -654.321}, "[1.23456e+02, -6.54321e+02]"},
140+
{"Simple Float64 Array", common.ValueTypeFloat64Array, []float64{123456789.0987654321, -987654321.123456789}, "[1.2345678909876543e+08, -9.876543211234568e+08]"},
141141
}
142142

143143
for _, tt := range tests {

0 commit comments

Comments
 (0)