Converting delimited number to float/Decimal

Hi there,

There are libraries that format float or Decimal to say comma delimited binary. Is there a built in function or library that can do the opposite? Meaning to convert a comma delimited binary to float/Decimal?

E.g: function to convert binary “1,999.00” to 1999.00 (either in float or Decimal)


Is this sufficient?

iex(1)> "1,999.00" |> String.replace(",", "") |> Float.parse
{1999.0, ""}

I haven’t worked with any decimal libraries, but if they support base-10 parsing and printing I believe the same comma stripping strategy should work well.

It wouldn’t work with all locales, though. Not aware of any libraries for that, but I haven’t needed it yet either.

1 Like

Yea, the only stopper is locale. Formatting from float to delimited binary is relatively easier because we specify the delimiter or pattern. The opposite way is more complex because locale is needed. For now, I have settled with a solution to only accept user input that is without delimiter.

Thanks very much for your quick reply!

1 Like

Or use String.to_float

"1,999.00" |> String.replace(",","") |> String.to_float

1 Like