From 33c8b9be0b3e60bfd95cd43cbee26f9e7ee220e4 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 15 Oct 2023 11:18:57 -0600 Subject: [PATCH] implement amount.go:Amount:FormatUSD with localizedLanguagePrinter --- digits-work-sample-go.d/amount.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/digits-work-sample-go.d/amount.go b/digits-work-sample-go.d/amount.go index 52fd4b4..31322fc 100644 --- a/digits-work-sample-go.d/amount.go +++ b/digits-work-sample-go.d/amount.go @@ -52,7 +52,14 @@ func (amount Amount) Rounded(places int) Amount { // FormatUSD formats Amount as a string in US dollars. func (amount Amount) FormatUSD() string { - // TODO: Not implemented. - // Hint: Number localization gets tricky. Can you find a library to help? - return "" + rounded := amount.Rounded(2) + prefix := "" + 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) }