generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package protomoney | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
|
||
"google.golang.org/genproto/googleapis/type/money" | ||
) | ||
|
||
// Fmt wraps a money value in a formatter allows it to be formatted using | ||
// standard fmt.Printf() verbs. | ||
func Fmt(m *money.Money) fmt.Formatter { | ||
return formatter{m} | ||
} | ||
|
||
type formatter struct { | ||
value *money.Money | ||
} | ||
|
||
func (f formatter) Format(st fmt.State, verb rune) { | ||
m := normalize(f.value) | ||
|
||
switch verb { | ||
case 'f', 'F', // decimal notation | ||
'e', 'E', // scientific notation | ||
'g', 'G', // decimal notation, or scientific for large exponents | ||
'v': // "default" format | ||
// This is a subset of the verbs are supported by big.Float.Format(). | ||
|
||
s := fmt.Sprintf("%d.%09d", m.GetUnits(), m.GetNanos()) | ||
f := &big.Float{} | ||
f.SetString(s) | ||
|
||
fmt.Fprintf(st, "%s ", m.GetCurrencyCode()) | ||
f.Format(st, verb) | ||
|
||
default: | ||
fmt.Fprintf(st, "%%!%c(*money.Money=%s)", verb, f.value.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package protomoney_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "github.com/dogmatiq/dosh/protomoney" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"google.golang.org/genproto/googleapis/type/money" | ||
) | ||
|
||
func ExampleFmt() { | ||
m := &money.Money{ | ||
CurrencyCode: "XYZ", | ||
Units: 10, | ||
Nanos: 129000000, | ||
} | ||
|
||
fmt.Printf("%.2f\n", Fmt(m)) | ||
// Output: XYZ 10.13 | ||
} | ||
|
||
var _ = Describe("func Fmt()", func() { | ||
It("returns a formatted representation of the amount", func() { | ||
m := &money.Money{ | ||
CurrencyCode: "XYZ", | ||
Units: 10, | ||
Nanos: 129000000, | ||
} | ||
s := fmt.Sprintf("%0.2f", Fmt(m)) | ||
Expect(s).To(Equal("XYZ 10.13")) | ||
}) | ||
|
||
It("returns a descriptive string if used with an unsupported verb", func() { | ||
m := &money.Money{ | ||
CurrencyCode: "XYZ", | ||
Units: 10, | ||
Nanos: 129000000, | ||
} | ||
s := fmt.Sprintf("%d", Fmt(m)) | ||
Expect(s).To(Equal("%!d(*money.Money=" + m.String() + ")")) | ||
}) | ||
}) |