In this core java tutorial we will learn how to convert String to float primitive type in java and convert String to float object with program and examples in java. We will also understand When to use convert String to float primitive type and when when to convert String to float object in java.
Programs to convert String to float in java -
- Program/Example 1 to How to convert String to float primitive type in java.
- Program/Example 2 to How to convert String to float object in java.
Program/Example 1 to How to convert String to float primitive type in java
package convertStringToFloatPkg;
public class ConvertStringToFloatExample1 {
public static void main(String[] args) {
String string = "12";
try {
float l = Float.parseFloat(string);
System.out.println("float number = " + l);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
/*OUTPUT
float number = 12.0
*/
|
If in case invalid string is passed which can't get converted to float will throw NumberFormatException in java.
Program/Example 2 to How to convert String to float object in java
Float.valueOf(string) method returns float object.
package convertStringToFloatPkg;
public class ConvertStringToFloatExample2 {
public static void main(String[] args) {
String string = "12";
Float floatObject = Float.valueOf(string);
System.out.println("float floatObject = " + floatObject);
}
}
/*OUTPUT
float floatObject = 12.0
*/
|
Programs to convert float to String in java -
- Program/Example 3 to How to convert float primitive type to String in java.
- Program/Example 4 to How to convert float object to String in java
Now, in this core java tutorial we will learn how to convert float primitive type to String in java and convert float object to String with program and examples in java. We will also understand When to use convert String to float primitive type and when when to convert String to float object in java.
Program/Example 3 to How to convert float primitive type to String in java.
String.valueOf(float) method converts float primitive type to String in java.
package convertFloatToStringPkg;
public class ConvertFloatToStringExample1 {
public static void main(String[] args) {
float f = 12f;
String string = String.valueOf(f);
System.out.println("string = "+string);
}
}
/*OUTPUT
string = 12.0
*/
|
Program/Example 4 to How to convert float object to String in java
toString() method of java.lang.Float class convert float object to String in java.
package convertFloatToStringPkg;
public class ConvertFloatObjectToStringExample2 {
public static void main(String[] args) {
Float floatObject = 12f;
String string = floatObject.toString();
System.out.println("string = "+string);
}
}
/*OUTPUT
string = 12.0
*/
|
Summary -
- In this core java tutorial we learned conversion of String to float primitive type in java and convert String to float object with program and examples in java.
- we also learned conversion of float primitive type to String in java and convert float object to String with program and examples in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy(JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>