Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short way to convert string to int

I usually do this to convert string to int:

my_input = int(my_input) 

but I was wondering if there was a less clumsy way, because it feels kind of long.

like image 998
Sam Banks Avatar asked Jan 24 '17 19:01

Sam Banks


People also ask

How do I convert a string to an int?

Use Integer.parseInt() to Convert a String to an Integer This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.

What's the fastest way to convert a string to number?

Use the parseInt() function to convert a string to a number, e.g. const num1 = parseInt(str) .

Which function converts a string to an integer?

The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.

Is atoi fast?

Atoi is the fastest I could come up with. I compiled with msvc 2010 so it might be possible to combine both templates. In msvc 2010, when I combined templates it made the case where you provide a cb argument slower.


1 Answers

my_input = int(my_input) 

There is no shorter way than using the int function (as you mention)

like image 189
Gonzalo Avatar answered Oct 11 '22 11:10

Gonzalo