Sanjibukai
Struggling with currency/amount input fields (Money)
Hello all,
I have a form with one price input field.
In order to follow industry standards (and for learning purpose) I’m using Money.
Currently I’m only dealing with only one currency (EUR), so I’m not into currency conversions etc.
I intended to use it only for the ease of formatting purpose as well as for the precision handling.
That being said, outputting a Money field is nice when displaying the value.
But I’m struggling for getting the value from a form.
At first I was not sure if I should use a text input field or an number input field.
It turns out that by the Money type is considered as a string.
But I had to disable the currency display since I already have a custom css displaying the currency in a styled way. So I’m basically using only the amount part.
But in that case, I wonder that I can simply go with a number field with the benefit to display a dedicated number keyboard in mobile.
The problem is that if I’m using a phone that has an English local, I cannot use commas but only dots as decimal separator.
And in this case if I put 12.34 I am ending up with 1234.00
When I’m using a text field it correctly handles the comma but not the dot.
Is it possible to set both the comma and the dot as the decimal separator?
How do you guys handle those pesky formatting problems?
Thanks for any comments..
Most Liked
kip
ex_money (I am the author) can parse input numbers in any of its known locales using the locale-specific separators. Including parsing amounts with the currency sign since, as you point out, the display output most commonly uses that. Here are some example that might help:
# Default to EUR since thats the default currency for Germany
iex> Money.parse "10.000,43", locale: "de"
#Money<:EUR, 10000.43>
# Parse an accounting amount
iex> Money.parse "(10.000,43)", locale: "de"
#Money<:EUR, -10000.43>
# With a symbol
iex> Money.parse "€10.000,43", locale: "de"
#Money<:EUR, 10000.43>
# Currency is specified
iex> Money.parse "CHF 10.000,43", locale: "de"
#Money<:CHF, 10000.43>
kip
ex_money version 5.3.0 is published to hex. Really appreciate the collaboration and patience, thanks. Here’s the changelog entry:
Bug Fixes
- Fix parsing money amounts to use Unicode definition of whitespace (set
[:Zs:]). Thanks to @Sanjibukai for the report.
Enhancements
- Add
Money.sum/2to sum a list ofMoney, converting them if required.
Upgrading
mix deps.update ex_money
kip
Sorry to hear you has an issue with ex_momey_sql. Please do raise an issue on github or DM me. I have had no issues reported so I want to quash this asap.
I believe the number input field in most (all?) browsers don’t allow the input of the separator, only the decimal point that is in the currently set locale for the browser. Most issues I’ve seen come from the server rendering a formatted money amount that includes currency symbols and/or separators that cannot then be edited correctly because of the restrictions on a number field.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









