bojanz / currency

Currency handling for Go.
https://pkg.go.dev/github.com/bojanz/currency
MIT License
557 stars 45 forks source link

Question: String value to currency and rate extraction #36

Closed bentcoder closed 2 months ago

bentcoder commented 2 months ago

Hi,

Does this library extract and output the numeric bit for the given string representation? See dummy examples below.

Thanks

out := some.Function("HK$2,990.12")
out.Symbol() -> "HK$"
out.Digit() -> "2,999"
out.Decimal() -> "12"
sym, dig, dec := some.Function("HK$2,990.12")
bojanz commented 2 months ago

The currency.Formatter has a Parse method you can use to get the amount and currency code. It's basically meant to be the opposite of Format().

It would not give you the symbol though, you would need custom logic for that.

DuncanSmith commented 2 months ago

Could you given an example of using the parse method for this use case please, even without the currency symbol. For example, if I have a value "0,49" EUR, passing this in to currency.NewAmount("0,49", "EUR") does not work; it's expecting "0.49". Can this be achieved by passing in localised formatting? It's what's being explained in this post https://stackoverflow.com/questions/45079074/parse-currency-float-string-to-float-type-based-on-locale-in-go

bojanz commented 2 months ago

There's an example in GoDoc: https://pkg.go.dev/github.com/bojanz/currency#example-Formatter.Parse

DuncanSmith commented 2 months ago

Ah, classic RTFM! 🤦‍♂️ Thank you.