Skip to content

Commit 09d4f93

Browse files
committed
Improve FormatZero.
1 parent 7f60118 commit 09d4f93

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type Accounting struct {
6363
Decimal string // decimal separator (optional / default: .)
6464
Format string // simple format string allows control of symbol position (%v = value, %s = symbol) (default: %s%v)
6565
FormatNegative string // format string for negative values (optional / default: strings.Replace(strings.Replace(accounting.Format, "-", "", -1), "%v", "-%v", -1))
66-
FormatZero string // format string for zero values (optional / default: %s0)
66+
FormatZero string // format string for zero values (optional / default: Format)
6767
}
6868
```
6969

@@ -75,7 +75,7 @@ Thousand | string | thousand separator | , | .
7575
Decimal | string | decimal separator | . | ,
7676
Format | string | simple format string allows control of symbol position (%v = value, %s = symbol) | %s%v | %s %v
7777
FormatNegative | string | format string for negative values | strings.Replace(strings.Replace(accounting.Format, "-", "", -1), "%v", "-%v", -1)) | %s (%v)
78-
FormatZero | string | format string for zero values | strings.Replace(accounting.Format, "%v", "0", -1) | %s --
78+
FormatZero | string | format string for zero values | Format | %s --
7979

8080
**Examples:**
8181

@@ -121,7 +121,7 @@ fmt.Println(ac.FormatMoney(0)) // "GBP --"
121121

122122
## FormatMoneyBigFloat(value *big.Float) string
123123

124-
** (>= Go 1.5) **
124+
**(>= Go 1.5)**
125125

126126
FormatMoneyBigFloat only supports [*big.Float](https://golang.org/pkg/math/big/#Float) value. It is faster than FormatMoney, because it does not do any runtime type evaluation.
127127

@@ -238,7 +238,7 @@ fmt.Println(accounting.FormatNumber(1000000, 3, " ", ",")) // "1 000 00
238238

239239
## FormatNumberBigFloat(value *big.Float, precision int, thousand string, decimal string) string
240240

241-
** (>= Go 1.5) **
241+
**(>= Go 1.5)**
242242

243243
FormatNumberBigFloat only supports [*big.Float](https://golang.org/pkg/math/big/#Float) value. It is faster than FormatNumber, because it does not do any runtime type evaluation.
244244

accounting.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type Accounting struct {
1212
Decimal string // decimal separator (optional / default: .)
1313
Format string // simple format string allows control of symbol position (%v = value, %s = symbol) (default: %s%v)
1414
FormatNegative string // format string for negative values (optional / default: strings.Replace(strings.Replace(accounting.Format, "-", "", -1), "%v", "-%v", -1))
15-
FormatZero string // format string for zero values (optional / default: strings.Replace(accounting.Format, "%v", "0", -1))
15+
FormatZero string // format string for zero values (optional / default: Format)
1616
}
1717

1818
func (accounting *Accounting) init() {
@@ -33,17 +33,22 @@ func (accounting *Accounting) init() {
3333
}
3434

3535
if accounting.FormatZero == "" {
36-
accounting.FormatZero = strings.Replace(accounting.Format, "%v", "0", -1)
36+
accounting.FormatZero = accounting.Format
3737
}
3838
}
3939

4040
func (accounting *Accounting) formatMoneyString(formattedNumber string) string {
4141
var format string
4242

43+
zero := "0"
44+
if accounting.Precision > 0 {
45+
zero += "." + strings.Repeat("0", accounting.Precision)
46+
}
47+
4348
if formattedNumber[0] == '-' {
4449
format = accounting.FormatNegative
4550
formattedNumber = formattedNumber[1:]
46-
} else if formattedNumber == "0" {
51+
} else if formattedNumber == zero {
4752
format = accounting.FormatZero
4853
} else {
4954
format = accounting.Format

accounting_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ func TestFormatMoney(t *testing.T) {
4444
AssertEqual(t, accounting.FormatMoney(1000000), "GBP 1,000,000")
4545
AssertEqual(t, accounting.FormatMoney(-5000), "GBP (5,000)")
4646
AssertEqual(t, accounting.FormatMoney(0), "GBP --")
47+
48+
accounting = Accounting{Symbol: "GBP", Precision: 2,
49+
Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
50+
AssertEqual(t, accounting.FormatMoney(1000000), "GBP 1,000,000.00")
51+
AssertEqual(t, accounting.FormatMoney(-5000), "GBP (5,000.00)")
52+
AssertEqual(t, accounting.FormatMoney(0), "GBP --")
4753
}
4854

4955
func TestFormatMoneyInt(t *testing.T) {

0 commit comments

Comments
 (0)