How to convert 2000 to 20.00

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…

7 Likes

2000 / 100.0 |> :erlang.float_to_binary([decimals: 2])

5 Likes

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.

10 Likes

This, exceptionally this, can never be stated enough!

6 Likes

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"
1 Like

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)

6 Likes

Kip, took a look at ex_money. Really amazing job you did on that.

1 Like