implement amount.go:Amount:FormatUSD with localizedLanguagePrinter

main
bel 2023-10-15 11:18:57 -06:00
parent ea64f6ea98
commit 33c8b9be0b
1 changed files with 10 additions and 3 deletions

View File

@ -52,7 +52,14 @@ func (amount Amount) Rounded(places int) Amount {
// FormatUSD formats Amount as a string in US dollars. // FormatUSD formats Amount as a string in US dollars.
func (amount Amount) FormatUSD() string { func (amount Amount) FormatUSD() string {
// TODO: Not implemented. rounded := amount.Rounded(2)
// Hint: Number localization gets tricky. Can you find a library to help? prefix := ""
return "" if rounded < 0 {
prefix = "-"
rounded *= -1.0
}
roundedAsCurrency := currency.NarrowSymbol(currency.USD.Amount(float32(rounded)))
resultWithSpaceAfterDollarSign := localizedLanguagePrinter.Sprintf("%s%.2f", prefix, roundedAsCurrency)
return strings.Replace(resultWithSpaceAfterDollarSign, "$ ", "$", 1)
} }