Converting from String to Integer or Float

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, themethod:

  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’llneed to compose it with another conversion:
int i = intValue(Integer.valueOf(“1”)); 
For floats, you need to first convert to a Float, thenconvert to a float:
float f = floatValue(Float.valueOf(“155.230”)); 
Java also provide a parseInt method. Try this:
   int i = Integer.parseInt(“1”); 

See also  What Are International Payments, And How Do They Work?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist