Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I parse "$1000.00" in current versions? #423

Open
lvh opened this issue Nov 16, 2024 · 1 comment
Open

How do I parse "$1000.00" in current versions? #423

lvh opened this issue Nov 16, 2024 · 1 comment

Comments

@lvh
Copy link

lvh commented Nov 16, 2024

I'm trying to parse some USD amounts formatted like "$1000" or "$1000.00". I know, from context, they're USD--I understand $ is used for other currencies too. I upgraded from moneta 1.1 to .1.4.4, which broke this previously working code. (The code is in Clojure but the meaning is presumably obvious if you're familiar with the Java equivalent.)

(def us-format
  (->
   (AmountFormatQueryBuilder/of Locale/US)
   (.set CurrencyStyle/SYMBOL)
   (.build)
   (MonetaryFormats/getAmountFormat)))

Parsing a string like $1000.00 results in the following error:

Unhandled javax.money.format.MonetaryParseException
   $ is not a unique currency symbol.

Googling the error points to #274, but that appears to mostly be the ticket that introduced the issue. That ticket seems to suggest there's no solution, but I don't know if it's simply outdated.

I submit that in the context of a US Locale, "$1000.00" is relatively unambiguous: it clearly means USD (the same way that in Canada a reasonable person would expect it to mean CAD). But, I'm fine with explicitly specifying the currency since I know what it is:

(def usd
  (Monetary/getCurrency "USD" (into-array String [])))

(def us-format
  (->
   (AmountFormatQueryBuilder/of Locale/US)
   (.set CurrencyStyle/SYMBOL)
   (.set CurrencyUnit usd) ;; <= new line
   (.build)
   (MonetaryFormats/getAmountFormat)))

However, this also doesn't work, with the same error. I submit that "the currency code is unambiguous" is irrelevant: I already specified what the currency is unambiguously. When I call .format on that AmountFormat it seems more than happy to give me $ back.

How do I parse $1000.00 in Moneta?

@lvh lvh changed the title How do I parse "$1000.00" now? How do I parse "$1000.00" in current versions? Nov 16, 2024
@sernamar
Copy link
Contributor

sernamar commented Nov 30, 2024

I’ve encountered a similar issue before, and as far as I know, beyond the straightforward approach of (clojure.string/replace amount "$" "USD"), there isn’t a built-in way in this library to handle this scenario directly for now.

As a temporary solution (and for fun), I ended up writing my own Clojure library for working with money, dinero, which might be of interest to you. One of its features is the ability to specify or infer the currency during parsing. For example:

(require '[dinero.parse :as parse])

(parse/parse-string "$1000" :locale java.util.Locale/US :currencies [:usd])
;;=> {:amount 1000M, :currency :usd}

(parse/parse-string "$1000" :locale java.util.Locale/US :try-all-currencies? true)
;;=> {:amount 1000M, :currency :usd}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants