Java – Convert Float to String
To convert a float value to a String in Java, you can use Float.toString() or String.valueOf(). String concatenation and StringBuffer.append() also produce a string representation, while formatting methods are useful when you need a fixed number of decimal places.
This tutorial shows each approach with Java examples and explains when to use plain conversion versus formatted output.
1. Convert float to String using Float.toString()
Float.toString(float) is a static method of the Float class. Pass the float value as the argument, and the method returns its string representation.
String str = Float.toString(floatValue);
In the following example, two float values are converted to strings using Float.toString().
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
The second output has fewer displayed digits because a Java float stores a limited-precision binary floating-point value. Float.toString() returns a decimal representation of the value actually stored in the float.
2. Convert float to String using string concatenation
A float can also be converted to a string by concatenating it with an empty string. When one operand of + is a string, Java performs string concatenation and converts the other operand to text.
This approach is concise, but Float.toString() or String.valueOf() usually expresses an explicit conversion more clearly.
The following example concatenates a float value with "".
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()
String.valueOf(float) is another direct way to convert a primitive float to a String. Pass the float value to the method and store the returned string.
String str = String.valueOf(floatValue);
For a primitive float, String.valueOf(float) produces the same kind of decimal representation as Float.toString(float).
In the following example, a float value is converted using String.valueOf().
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);
}
}
The value stored in str is the string "52.4839".
4. Convert float to String using StringBuffer.append()
StringBuffer.append(float) appends the string representation of a float to a buffer. Calling toString() on that buffer then returns a String.
This technique is more useful when you are already building a larger string from several values. For a single float conversion, Float.toString() or String.valueOf() is simpler.
The following example appends each float to a new StringBuffer and then calls toString().
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
5. Convert float to String with 2 decimal places in Java
If the goal is to display a float with exactly two digits after the decimal point, use formatting rather than a plain conversion. String.format() can format and round the value to two decimal places.
The format specifier %.2f means: format a floating-point value with two digits after the decimal point.
import java.util.Locale;
public class FloatToStringTwoDecimals {
public static void main(String[] args) {
float value = 12.3456f;
String str = String.format(Locale.ROOT, "%.2f", value);
System.out.println(str);
}
}
Output
12.35
Here, 12.3456f is formatted as "12.35". This is different from Float.toString(): formatting controls how the number is displayed, while plain conversion returns a normal string representation of the stored float value.
Choosing a Java float-to-String conversion method
| Method | Best suited for |
|---|---|
Float.toString(value) | Direct, explicit conversion from float to String |
String.valueOf(value) | Direct conversion when using the general String.valueOf() family |
value + "" | Short concatenation expressions, though less explicit for conversion |
StringBuffer.append(value).toString() | Building a string that already uses a StringBuffer |
String.format(...) | Controlling decimal places and display formatting |
Java float-to-String conversion summary
For a straightforward float-to-string conversion, use Float.toString() or String.valueOf(). Concatenation also works, and StringBuffer.append() is useful when a buffer is already being used to assemble text. When you need a specific display format such as two decimal places, use a formatting method such as String.format().
In this Java Tutorial, we learned how to convert a float to a String in Java and how formatted conversion differs from plain string conversion.
TutorialKart.com