Java – Convert Float to String
You can convert an floating point number to string in Java using String class methods.
In this tutorial, we shall go through some of the ways to convert a float to string, with example Java programs.
1. Convert float to string using Float.toString() method
toString() is a static method in Float class that returns string object representing the specified floating point value. We have to pass the float value as argument to toString() method.
In the following example we shall convert a number of float datatype to string by using Float.toString() function.
Java Program
/**
* Java Program - Convert Float to String
*/
public class FloatToString {
public static void main(String[] args) {
String str = Float.toString(1.63869f);
System.out.println(str);
str = Float.toString(-25.963869f);
System.out.println(str);
}
}
Output
1.63869
-25.96387
2. Convert float to string using String Concatenation
This is the most easiest way to convert an floating point number to a string. All you have to do is add an empty string to it.
The concatenation operator takes float and empty string as operands and returns a string.
In the following example we shall convert a float to string by concatenating the number to an empty string.
Java Program
/**
* Java Program - Convert Float to String
*/
public class FloatToString {
public static void main(String[] args) {
float n = 52.52639f;
//convert float to string using string concatenation
String str = n + "";
System.out.print(str);
}
}
Output
52.52639
3. Convert float to string using String.valueOf() method
We can use String.valueOf() method to get a string value from a floating point number. Pass the float as argument to String.valueOf() function, and the function returns a String object.
In the following example we shall convert a number of float datatype to string by using String.valueOf() function.
Java Program
/**
* Java Program - Convert Float to String
*/
public class FloatToString {
public static void main(String[] args) {
float n = 52.4839f;
//convert float to string using String methods
String str = String.valueOf(n);
System.out.print(str);
}
}
Float value has been converted to String.
4. Convert float to string using StringBuffer.append() method
Create a StringBuilder object and append the float value to it using append() method. append() method appends the string representation of the float to this sequence. After append() function, call toString() method on the StringBuilder object, it returns string.
You can create StringBuilder object and call append() function and toString() function as chain in a single statement.
In the following example we shall convert a number of float datatype to string by using StringBuilder class.
Java Program
/**
* Java Program - Convert Float to String
*/
public class FloatToString {
public static void main(String[] args) {
String str = (new StringBuffer()).append(1.63869f).toString();
System.out.println(str);
str = (new StringBuffer()).append(-25.963869f).toString();
System.out.println(str);
}
}
Output
1.63869
-25.96387
Conclusion
In this Java Tutorial, we learned how to convert or typecast a float to string in Java, using String concatenation or String.valueOf() method.