Skip to content

Commit

Permalink
Implement fmt.Formatter
Browse files Browse the repository at this point in the history
* Fixes shopspring#326
* We would like to get started implement the fmt.Formatter.
* At first, we implement the indicators of '%s' and '%f'.
  • Loading branch information
u5surf committed Apr 23, 2024
1 parent a1bdfc3 commit 67840ae
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
10 changes: 10 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,16 @@ func abs(n int32) int32 {
return n
}

func (d Decimal) Format(f fmt.State, verb rune) {
switch verb {
case 'f':
places, _ := f.Precision()
fmt.Fprintf(f, d.StringFixed(int32(places)))
case 's':
fmt.Fprintf(f, d.String())
}
}

// Cmp compares the numbers represented by d and d2 and returns:
//
// -1 if d < d2
Expand Down
61 changes: 61 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,67 @@ func TestDecimal_RoundCeilAndStringFixed(t *testing.T) {
}
}

func TestDecimal_Format(t *testing.T) {
type testData struct {
input string
places int32
expected string
}
tests := []testData{
{"1.454", 0, "1"},
{"1.454", 1, "1.5"},
{"1.454", 2, "1.45"},
{"1.454", 3, "1.454"},
{"1.454", 4, "1.4540"},
{"1.454", 5, "1.45400"},
{"0.454", 0, "0"},
{"0.454", 1, "0.5"},
{"0.454", 2, "0.45"},
{"0.454", 3, "0.454"},
{"0.454", 4, "0.4540"},
{"0.454", 5, "0.45400"},
{"0", 0, "0"},
{"0", 1, "0.0"},
{"0", 2, "0.00"},
{"5", 2, "5.00"},
{"5", 1, "5.0"},
{"5", 0, "5"},
{"500", 2, "500.00"},
{"1.1001", 2, "1.10"},
{"-1.1001", 2, "-1.10"},
{"-1.454", 0, "-1"},
{"-1.454", 1, "-1.5"},
{"-1.454", 2, "-1.45"},
{"-1.454", 3, "-1.454"},
{"-1.454", 4, "-1.4540"},
{"-1.454", 5, "-1.45400"},
{"-1.554", 0, "-2"},
{"-1.554", 1, "-1.6"},
{"-1.554", 2, "-1.55"},
{"-0.554", 0, "-1"},
{"-0.454", 0, "0"},
{"-0.454", 5, "-0.45400"},
{"-500", 2, "-500.00"},
}

for _, test := range tests {
d, err := NewFromString(test.input)
if err != nil {
t.Fatal(err)
}
_, err = NewFromString(test.expected)
if err != nil {
t.Fatal(err)
}
formatIndicator := "%." + fmt.Sprintf("%d", test.places) + "f"
got := fmt.Sprintf(formatIndicator, d)
if got != test.expected {
t.Errorf("format indicator: %s: got %s, expected %s",
formatIndicator, got, test.expected)
}
}
}

func TestDecimal_RoundFloorAndStringFixed(t *testing.T) {
type testData struct {
input string
Expand Down

0 comments on commit 67840ae

Please sign in to comment.