From bf0462d6cad8adee9d5db57df264ddf2df4d8d85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20Soul=C3=A9?= Date: Wed, 18 Sep 2024 21:00:41 +0200 Subject: [PATCH] fix: seems vitodata server expects floats with "," as decimal separator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #15. Signed-off-by: Maxime Soulé --- types.go | 11 ++++++++--- types_test.go | 10 +++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/types.go b/types.go index 192c16b..2343f85 100644 --- a/types.go +++ b/types.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strconv" + "strings" ) // Singletons matching Vitodata™ types. @@ -56,19 +57,23 @@ func (v *VitodataDouble) Human2VitodataValue(value string) (string, error) { if err != nil { return "", err } - return strconv.FormatFloat(num, 'f', -1, 64), nil + return strings.Replace(strconv.FormatFloat(num, 'f', -1, 64), ".", ",", 1), nil } // Vitodata2HumanValue checks that the value is a float number and // returns it after reformatting. func (v *VitodataDouble) Vitodata2HumanValue(value string) (string, error) { - return v.Human2VitodataValue(value) + num, err := strconv.ParseFloat(strings.Replace(value, ",", ".", 1), 64) + if err != nil { + return "", err + } + return strconv.FormatFloat(num, 'f', -1, 64), nil } // Vitodata2NativeValue extract the number from the passed string and // returns it as a float64. func (v *VitodataDouble) Vitodata2NativeValue(value string) (interface{}, error) { - num, err := strconv.ParseFloat(value, 64) + num, err := strconv.ParseFloat(strings.Replace(value, ",", ".", 1), 64) if err != nil { return nil, err } diff --git a/types_test.go b/types_test.go index cac1d4d..a1a7766 100644 --- a/types_test.go +++ b/types_test.go @@ -14,7 +14,7 @@ func TestVitodataDouble(tt *testing.T) { // Human2VitodataValue str, err := TypeDouble.Human2VitodataValue("1.200") - t.CmpDeeply(str, "1.2") + t.CmpDeeply(str, "1,2") t.CmpNoError(err) str, err = TypeDouble.Human2VitodataValue("foo") @@ -26,6 +26,10 @@ func TestVitodataDouble(tt *testing.T) { t.CmpDeeply(str, "1.2") t.CmpNoError(err) + str, err = TypeDouble.Vitodata2HumanValue("1,200") + t.CmpDeeply(str, "1.2") + t.CmpNoError(err) + str, err = TypeDouble.Vitodata2HumanValue("foo") t.Empty(str) t.CmpError(err) @@ -35,6 +39,10 @@ func TestVitodataDouble(tt *testing.T) { t.CmpDeeply(num, float64(1.2)) t.CmpNoError(err) + num, err = TypeDouble.Vitodata2NativeValue("1,200") + t.CmpDeeply(num, float64(1.2)) + t.CmpNoError(err) + num, err = TypeDouble.Vitodata2NativeValue("foo") t.Nil(num) t.CmpError(err)