Introduction
In Java, there are multiple ways to convert a float to a string. In this lab, we will discuss three different methods of converting Float to String in Java. The three methods that will be covered are: using the valueOf() method of the String class, using the toString() method of the Float class, and using string literals.
Declare the Float Value
Declare and initialize a float variable named floatValue with a value of 10.5.
float floatValue = 10.5f;
Convert Float to String using valueOf() Method
Use the valueOf() method of the String class to convert the floatValue to a string.
String strValue = String.valueOf(floatValue);
Print the String Value
Print the strValue and its datatype.
System.out.println(strValue);
System.out.println("Data Type: " + strValue.getClass().getName());
Convert Float to String using toString() Method
Use the toString() method of the Float class to convert the floatValue to a string.
String strValue2 = Float.toString(floatValue);
Print the String Value
Print the strValue2 and its datatype.
System.out.println(strValue2);
System.out.println("Data Type: " + strValue2.getClass().getName());
Convert Float to String using String literals
Use the string literals to convert the floatValue to a string.
String strValue3 = "" + floatValue;
Print the String Value
Print the strValue3 and its datatype.
System.out.println(strValue3);
System.out.println("Data Type: " + strValue3.getClass().getName());
Compile and Run the Code
To compile the code use the following command:
javac FloatToString.java
To run the compiled code use this command:
java FloatToString
Summary
In this lab, we discussed three different methods to convert Float to String in Java. The conversion methods discussed were using the valueOf() method of the String class, using the toString() method of the Float class, and using string literals. By using these methods, a floating-point value can be converted into a string value in Java.



