As mentioned by @Khashayar, the problem in your code is that string:to_integer/1
returns a pair (a tuple with two elements) and the integer is the first element.
However, you should not use this function. A string in Erlang is just a list of integers, and what you meant to use is list_to_integer/1
. This is the common way to convert a string to an integer.
If you used list_to_integer/1
, you would have avoided the bug in @Khashayar's code where the second pair element is matched with anything. Indeed, you can enter the following:
Calculate the cost of flooring based on width, height, and cost per square foot.
Width in feet: 1.9
Height in feet: 1.9
Cost in dollars per square foot: $ 4.9
4
While 1.9*1.9*4.9
actually equals 17.689
.
Unfortunately, there is no list_to_number/1
function which would return either an integer or a float. The most common way to handle this is to perform a try/catch with list_to_float/1
and fallback to list_to_integer/1
. Alternatively, you could use io_lib:fread/2
or string:to_float/1
which do not raise exceptions (still, as mentioned above, using string:to_float/1
is judged a bad practice).