Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string number to integer number [duplicate]

Tags:

java

I have "1" as a string, and I would like to convert it to decimal, 1, as integer.

I tried charAt(), but it returns 49, not 1 integer.

So, what is required to convert the "1" string to 1 integer?

like image 277
Chriss Avatar asked Sep 30 '13 20:09

Chriss


People also ask

How can a string be converted to a number?

You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.

How do I convert a string to an int in C++?

One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.


1 Answers

Use Wrapper Class.

Examples are below

int

int a = Integer.parseInt("1"); // Outputs 1 

float

float a = Float.parseFloat("1"); // Outputs 1.0 

double

double a = Double.parseDouble("1"); // Outputs 1.0 

long

long a = Long.parseLong("1"); // Outputs 1 
like image 188
KhAn SaAb Avatar answered Oct 12 '22 09:10

KhAn SaAb