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)