Skip to content

Commit

Permalink
accept amounts with commas
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardini687 committed Aug 4, 2021
1 parent 65e65b9 commit e29a775
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
7 changes: 4 additions & 3 deletions kakebo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
25 changes: 22 additions & 3 deletions kakebo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, `<nil>`", got, err, want)
}
}

func TestFormatEntriesFieldsErr(t *testing.T) {
data := `1.2 foo
3.45
Expand Down Expand Up @@ -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, `<nil>`", got, err, want)
if got != want {
t.Fatalf("\n GOT: %#v\nWANT: %#v", got, want)
}
}

0 comments on commit e29a775

Please sign in to comment.