Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to int conversion using toInt function

Tags:

elm

I am trying to convert a string to integer using String.toInt. However, when I want to bind the result to a variable and then do some simple math with it I get this error:

Function add is expecting the 2nd argument to be:

Int 

But it is:

Result String Int 

How can I just extract the integer part of the result?

like image 308
Mahdi Avatar asked Dec 18 '15 19:12

Mahdi


People also ask

Which function converts a string to an integer?

The atoi() function converts a character string to an integer value.

Can we convert string to int in java?

The method generally used to convert String to Integer in Java is parseInt() of String class.


1 Answers

Here's how to supply the conversion with a default value in case the parsing fails.

String.toInt "5" |> Result.toMaybe |> Maybe.withDefault 0 
like image 85
d13 Avatar answered Sep 23 '22 12:09

d13