You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.)
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 USLocale, "$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:
(defusd
(Monetary/getCurrency"USD" (into-array String [])))
(defus-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?
The text was updated successfully, but these errors were encountered:
lvh
changed the title
How do I parse "$1000.00" now?
How do I parse "$1000.00" in current versions?
Nov 16, 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:
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.)Parsing a string like
$1000.00
results in the following error: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: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?The text was updated successfully, but these errors were encountered: