Sanjibukai

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

kip

ex_cldr Core Team

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

kip

ex_cldr Core Team

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/2 to sum a list of Money, converting them if required.

Upgrading

mix deps.update ex_money
kip

kip

ex_cldr Core Team

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.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement