diff --git a/kakebo.go b/kakebo.go index 69b87d7..940071d 100644 --- a/kakebo.go +++ b/kakebo.go @@ -91,7 +91,7 @@ func CalcMonth(monthData string) (decimal.Decimal, error) { // // Tot 10,65 // -func DisplayMonth(period time.Time, monthData string, monthTot decimal.Decimal) (string, error) { +func DisplayMonth(period time.Time, monthData string, monthTot decimal.Decimal) string { var lines []string lines = append(lines, fmt.Sprintln(period.Month(), period.Year())) // header @@ -100,7 +100,7 @@ func DisplayMonth(period time.Time, monthData string, monthTot decimal.Decimal) display := strings.Join(lines, "\n") - return strings.ReplaceAll(display, ".", ","), nil + return strings.ReplaceAll(display, ".", ",") } // DisplayStats @@ -188,7 +188,8 @@ func formatEntry(fields []string) (string, error) { return "", fmt.Errorf("at least 2 fields required") } - amount, err := decimal.NewFromString(fields[0]) + s := strings.Replace(fields[0], ",", ".", 1) + amount, err := decimal.NewFromString(s) if err != nil { return "", err } diff --git a/kakebo_test.go b/kakebo_test.go index f434dc3..b8487f9 100644 --- a/kakebo_test.go +++ b/kakebo_test.go @@ -26,6 +26,25 @@ Xyzzy 78.09 } } +func TestFormatEntriesComma(t *testing.T) { + data := `1,2 foo +3,45 bar +6 baz +78,09 xyzzy +` + want := `Foo 1.20 +Bar 3.45 +Baz 6.00 +Xyzzy 78.09 +` + + got, err := FormatEntries(data) + + if got != want || err != nil { + t.Fatalf("\n GOT: %#v, `%v`\nWANT: %#v, ``", got, err, want) + } +} + func TestFormatEntriesFieldsErr(t *testing.T) { data := `1.2 foo 3.45 @@ -187,9 +206,9 @@ Tot 88,74 date := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) tot, _ := CalcMonth(monthData) - got, err := DisplayMonth(date, monthData, tot) + got := DisplayMonth(date, monthData, tot) - if got != want || err != nil { - t.Fatalf("\n GOT: %#v, `%v`\nWANT: %#v, ``", got, err, want) + if got != want { + t.Fatalf("\n GOT: %#v\nWANT: %#v", got, want) } }