Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java Language
Expertise: Beginner
Mar 21, 1997



Application Security Testing: An Integral Part of DevOps

Converting from String to Integer or Float

How can I convert a String type value to an integer or float? For example,
String str="155.230";
float  real;
real = str.HowCanIConvert???????;
I've tried it like this:
Integer aValue = new Integer(String.valueOf("1")); 
But this method doesn't always work. What's the problem?

You're lucky your solution worked at all! Probably, the method:

  public static String valueOf(Object obj)  
was being called, as I see no variant of String.valueOf() that expects a String parameter.

Perhaps you intended

Of course this only returns an Integer. To get an int, you'll need to compose it with another conversion:
int i = intValue(Integer.valueOf("1")); 
For floats, you need to first convert to a Float, then convert to a float:
float f = floatValue(Float.valueOf("155.230"));
Java also provide a parseInt method. Try this:
   int i = Integer.parseInt("1"); 
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date