Skip to content

Commit

Permalink
Merge pull request #14 from bykof/master
Browse files Browse the repository at this point in the history
Use formattedNumber to format zero
  • Loading branch information
leekchan authored Nov 4, 2019
2 parents a095955 + 9ff36f5 commit 0b9b0bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 2 additions & 5 deletions accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,12 @@ func (accounting *Accounting) init() {
func (accounting *Accounting) formatMoneyString(formattedNumber string) string {
var format string

zero := "0"
if accounting.Precision > 0 {
zero += "." + strings.Repeat("0", accounting.Precision)
}
formattedZero := FormatNumber(0, accounting.Precision, accounting.Thousand, accounting.Decimal)

if formattedNumber[0] == '-' {
format = accounting.FormatNegative
formattedNumber = formattedNumber[1:]
} else if formattedNumber == zero {
} else if formattedNumber == formattedZero {
format = accounting.FormatZero
} else {
format = accounting.Format
Expand Down
6 changes: 5 additions & 1 deletion accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestFormatMoney(t *testing.T) {
AssertEqual(t, accounting.FormatMoney(decimal.New(499999, -2)), "$4,999.99")
AssertEqual(t, accounting.FormatMoney(decimal.New(500000, 0)), "$500,000.00")

accounting = NewAccounting( "$", 0, ",", ".","%s %v","-%s %v","%s %v")
accounting = NewAccounting("$", 0, ",", ".", "%s %v", "-%s %v", "%s %v")
AssertEqual(t, accounting.FormatMoney(123456789.213123), "$ 123,456,789")
AssertEqual(t, accounting.FormatMoney(12345678), "$ 12,345,678")
AssertEqual(t, accounting.FormatMoney(-12345678), "-$ 12,345,678")
Expand All @@ -86,6 +86,10 @@ func TestFormatMoney(t *testing.T) {
AssertEqual(t, accounting2.FormatMoney(1000000), "GBP 1,000,000.00")
AssertEqual(t, accounting2.FormatMoney(-5000), "GBP (5,000.00)")
AssertEqual(t, accounting2.FormatMoney(0), "GBP --")

accounting2 = Accounting{Symbol: "€", Precision: 2,
Decimal: ",", FormatZero: "0.-"}
AssertEqual(t, accounting2.FormatMoney(0), "0.-")
}

func TestFormatMoneyInt(t *testing.T) {
Expand Down

0 comments on commit 0b9b0bd

Please sign in to comment.