JavaDevNotes.com

Java Float To String Examples

This post will show examples on how to convert Java Float to String. It is a commonly encountered case to convert a float value to String when programming with the Java language.
Here are some examples for performing the conversion:

Convert using Float.toString(float)

The Float class has a static method that returns a String object representing the specified float parameter. Using this is an efficient solution.

Syntax

public static String toString(float f)

This returns the string representation of the float argument. The argument f is converted to it's string representation and returns the String instance. If the number is negative, the sign will be preserved.

Example
float number = -895.25f;
String numberAsString = Float.toString(number);
The code is equivalent to:
String numberAsString = "-895.25";
This is the most common and popular solution for converting a float to String.


Convert using String.valueOf(float)

The String class has several static methods that can convert most primitive types to their String representation. This includes floats.

Example

float number = -895.25f;
String numberAsString = String.valueOf(number);
or
String numberAsString = String.valueOf( -895.25f);

This is also a very simple and popular solution for converting a float to String. This is also as efficient as the first solution shown above.


Convert using new Float(float).toString()

Another alternative method is to create an instance of Float class and then invoke it's toString() method. This is a little less efficient than the first two options shown above. This is because a new instance of Float is created before conversion is performed.

Example

float number = -895.25f;
Float floatInstance = new Float(number);      
String numberAsString = floatInstance.toString();

We can shortened to:

float number = -895.25f;
String numberAsString = new Float(number).toString();

or just:

String numberAsString = new Float(-895.25f).toString();

Usually, this method should be used when the variable you wish to convert is already an instance of the Float class. If you only have a primitive variable (float), use the first two options mentioned above.


Convert using String.format()

String.format() is a new alternative that can be used for converting a Float to String object. It is first introduced in Java 5 (JDK1.5) and has many cool features.

Syntax
public static String format(String format, Object... args)

There are many options on how to use this method. But for conversion purposes, here is a simple example:

Example
float number = -895.25f;
String numberAsString = String.format ("%f", number);

And the variable numberAsString will have the value "-895.250000"

We can also control the number of decimal places.

float number = -895.25f;
System.out.println(String.format ("%.1f", number));
System.out.println(String.format ("%.2f", number));
System.out.println(String.format ("%.3f", number));
Will display
-895.3
-895.25
-895.250

If you are using Java 5 or higher, this is also a very simple alternative for converting a float to String object.



Convert using DecimalFormat

The class java.text.DecimalFormat is a class that formats a number to a String representation following a certain pattern.
Example

float number = -100895.256f;
DecimalFormat decimalFormat = new DecimalFormat("#.00");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output
-100895.26

This is an example that will convert to String but with commas

float number = -100895.256f;
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
Will output
-100,895.26

This is ideal if you are looking for a solution where you have more control on the resulting String. You can specify comma separator for readability and control the number of decimal places.


Convert using StringBuffer or StringBuilder

StringBuffer is a class that is used to concatenate multiple values into a String. StringBuilder works similarly but not thread safe like StringBuffer. These two classes can be used to convert a Java Float to String.

StringBuffer Example

float number = -895.25f;
StringBuffer sb = new StringBuffer();
sb.append(number);
String numberAsString = sb.toString();

StringBuilder Example

float number = -895.25f;
StringBuilder sb = new StringBuilder();
sb.append(number);
String numberAsString = sb.toString();

Shortened Examples

String numberAsString = new StringBuffer().append(-895.25f).toString();

String numberAsString = new StringBuilder().append(-895.25f).toString();


Quick Solution

If you want something simple that works but not elegant, you can use this code:
float number = -895.25f;
String numberAsString = "" + number;

When you concatenate a String and a Float, the result is a String. Internally, the code is inefficient because there are intermediate objects that are created behind the scene.



String Tutorials And Examples