Currency format dynamic fractional digits
Closed this issue · 2 comments
gordoneliel commented
I'm using ex_money and formatting money in phoenix views and its fantastic.
The default formatting works great, except what I want is for the formatted money to not include fractional digits if there is none / is zero. Eg. $20.00 to be $20, but $20.01 should remain the same.
Is there an option that does this? I'm curious how others go about this.
kipcole9 commented
@gordoneliel there's no option to do what you're after automatically. However I have just published ex_money version 5.14.0 that includes a Money.integer?/1
function. Which means in your code you can do something like:
if Money.integer?(money) do
Money.to_string money, fractional_digits: 0
else
Money.to_string(money)
end
Which produces:
iex> MyApp.to_string Money.new(:USD, 100)
{:ok, "$100"}
iex> MyApp.to_string Money.new(:USD, "100.10")
{:ok, "$100.10"}
gordoneliel commented
That works! I'll give it a shot, thanks for the help.