Convert String to Float in Java
There are many ways to Convert String to Float and vice-versa like
String concatenation
Float.toString() method
Float.parseFloat(string)
String.valueOf() method
We see them one by one
Convert String to Float
package com.javacodestuffs.core.converters;
public class StringToFloat {
public static void main(String[] args) {
String floatString = "151.00";
try {
float ft = Float.valueOf(floatString.trim()).floatValue();
System.out.println("float string is : " + floatString);
System.out.println("float is : " + ft);
}
catch(NumberFormatException nfe) {
System.err.println("NumberFormatException: " + nfe.getMessage());
}
}
}
output:
$javac com/javacodestuffs/core/converters/StringToFloat.java
$java -Xmx128M -Xms16M com/javacodestuffs/core/converters/StringToFloat
float string is : 151.00
float is : 151.0
Using Float.parseFloat(string)
you can also convert a Java String to a Java float field as shown below example.
package com.javacodestuffs.core.converters;
public class ConvertStringToFloat {
public static void main(String[] args) {
try {
String floatString1 = "hello";
String floatString2 = "121.00";
//float fl1 = Float.parseFloat(string); you can't do it ,exception will thrown as java.lang.NumberFormatException: For input string: "foo"
float fl2 = Float.parseFloat(floatString2);
System.out.println("float string is : " + fl2);
}
catch(NumberFormatException ex) {
ex.printStackTrace();
}
}
}
output:
float string is : 121.0
Convert float to String
For converting float to string we can use String.valueOf(fl2) or toString() method.
Example1
package com.javacodestuffs.core.converters;
public class ConvertFloatToString1 {
public static void main(String[] args) {
float fl2 = 0.1235f;
String floatString = String.valueOf(fl2);
System.out.println("float is : " + fl2);
System.out.println("float equivalent to String : " + floatString);
}
}
output:
float is : 0.1235
float equivalent to String : 0.1235
Example2
package com.javacodestuffs.core.converters;
public class ConvertFloatToString2 {
public static void main(String[] args) {
Float fl = Float.valueOf(12.045f);
String floatString = fl.toString();
System.out.println("float is : " + fl);
System.out.println("float equivalent to String : " + floatString);
}
}
output:
float is : 12.045
float equivalent to String : 12.045
Post/Questions related to Convert String to Float in Java
Can you convert string to float?
Array to String in Java
How do you round a float in Java?
How do I convert a number to a string in Java?
What is the difference between double and float?
How to convert Map to List in Java
How to convert Double to Long in Java?
How to convert Array to String in Java?
Ways to convert Java 8 Stream to List?
How to convert a binary number to decimal in Java?
Examples of converting InputStream to String in Java?
How do you pass a float value in Java?
How to convert Character to String in Java?
Examples of converting Array to ArrayList in Java
How do you initialize a float?
How to convert ArrayList to Set in Java?
In this article, we have seen how to Convert String to Float in Java with examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment