I want to change format 2000 to 20.00
I want to put period “.” at last two digit in number
how can I do this?
It looks as if you were searching for a simple division by 100…
2000 / 100.0 |> :erlang.float_to_binary([decimals: 2])
I’d like to add teh gentle reminder that if you’re dealing with money values (which it always looks like if someone needs two decimal places) you should never use floats, but at least the Decimal library if not a dedicated money handling library.
This, exceptionally this, can never be stated enough!
Stupid/quick and dirty way of solving this, but avoids some issues wrt rounding and such:
> Regex.replace(~r/(-?\d+)(\d\d)/, "2000", "\\1.\\2")
"20.00"
If it is money you’re dealing with then ex_money would let you do the following:
iex> Money.from_integer(2000, :USD)
#Money<:USD, 20.00>
(I am the author)
Kip, took a look at ex_money. Really amazing job you did on that.