parsing numbers from strings

928 views
Skip to first unread message

Elliot Finley

unread,
Mar 1, 2015, 8:19:02 PM3/1/15
to elixir-l...@googlegroups.com
I have some strings that represent numbers.  The only catch is, I don't know ahead of time whether they contain a decimal point or not.

iex(12)> String.to_integer("45")
45
iex(13)> String.to_integer("45.0")
** (ArgumentError) argument error
    :erlang.binary_to_integer("45.0")


iex(11)> String.to_float("45.0")
45.0
iex(11)> String.to_float("45")
** (ArgumentError) argument error
    :erlang.binary_to_float("45")

I could write a string_to_num function that checks the string to see if it contains a period and then use the appropriate String.to_X function, but that seems very clumsy.

Any pointers on how to handle this would be appreciated.

Thanks,
Elliot

David Elkins

unread,
Mar 1, 2015, 9:57:33 PM3/1/15
to elixir-l...@googlegroups.com
A question for the experts, is it considered clumsy still if one does a try and rescue/catch instead of searching for period in string, eg

defmodule MyModule do
  def string_to_num(string) do
    try do
      String.to_integer(string)
    rescue
      ArgumentError -> String.to_float(string)
    end
  end
end

IO.puts inspect MyModule.string_to_num("45")
IO.puts inspect MyModule.string_to_num("45.0")


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CACRGtSNYFsTmYaR7%3Dz53DA_qTxE-CXAo795ACF_8HfOdK-gdDA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jason M Barnes

unread,
Mar 1, 2015, 10:03:30 PM3/1/15
to elixir-l...@googlegroups.com

defmodule MyString do
  def to_number(val) do
    num = String.to_char_list(val)
    case :string.to_float(num) do
      {:error, :no_float} -> :erlang.list_to_binary(num)
      {float, _} -> float
    end
  end
end

It does seem odd that String.to_float() won’t accept numbers without periods, though.

On Sun, Mar 1, 2015 at 8:19 PM, Elliot Finley <efinle...@gmail.com> wrote:

José Valim

unread,
Mar 2, 2015, 2:56:40 AM3/2/15
to elixir-l...@googlegroups.com
String.to_integer and String.to_float do conversions. If you want parsing of input, you should look at Integer.parse and Float.parse. Is it also able to return a float from Float.parse("1").



José Valim
Skype: jv.ptec
Founder and Lead Developer

greggreg

unread,
Mar 2, 2015, 10:33:45 AM3/2/15
to elixir-l...@googlegroups.com
defmodule MyString do
   
def to_number(val) do
 
     
case val =~ "." do   # <- check if it has a `.`
       
true  -> Float.parse val
       
false -> Integer.parse val
     
end
   
end
end

If you wanna just return the number and not the rest of the binary:

defmodule MyString do
   
def to_number(val) do
 
     num
= case val =~ "." do
       
true  -> Float.parse val
       
false -> Integer.parse val
     
end

     
case num do
     
:error   -> :error
     
{num, _} -> num
     
end
   
end
end

Note: Integer.parse and Float.parse can't handle the "readable" way to write numbers: Integer.parse("1_234_567") == {1, "_234_567"}
Reply all
Reply to author
Forward
0 new messages