loat is one of the most basic data type in Java for representing numerical values with decimal places. A common situation is when we want to convert a float value to String. Below are some examples on how to convert float to String in Java using two decimal places.
Convert using String.format()

For Java 5 and above, the String.format() can be utilized to convert a numeric value to its String representation. Below is an example on how to convert a float to String with
2 decimal places using String.format():

/**
* A simple Java example that converts a float to String using two decimal places via String.format().
*/
public class Test {
public static void main(String[] args) {
float number = 888.7893f;
String numberAsString = String.format ("%.2f", number);
System.out.println(numberAsString);
}
}

The “%.2f” passed to the format method indicates we wish to use two decimal places in the conversion. If the float contains more decimal digits like in the example, it is expected that the result will be rounded. Hence the output should be:

888.79

String.format() also works with thousand separatoor. Below is a modified example that converts a float to String with two decimal places and comma for every three digits in the whole number component::

/**
* A simple Java example that converts a float to String using two decimal places and using comma
* as thousand separator via String.format().

*/
public class Test {
public static void main(String[] args) {
float number = 12223.3655f;
String numberAsString = String.format ("%,.2f", number);
System.out.println(numberAsString);
}
}

Using “%,.2f” as parameter, the comma “,” means to use comma separator for every three digits on the left side of the dot, and the 2 after the dot “.” indicates two decimal places for the conversion. Below is the output of the code:

12,223.37

When working with floats, beware of the caveat that it only supports a smaller mantissa or precision for the digits. Hence the example below will display erratic result:

/**
* A simple Java example that converts a very large float to String using String.format().
*/
public class Test {
public static void main(String[] args) {
System.out.println(String.format ("%,.2f", 1222355.3655f));
System.out.println(String.format ("%,.2f", 1222355.6585f));
System.out.println(String.format ("%,.2f", 12223551.3655f));
System.out.println(String.format ("%,.2f", 12223551.6585f));
}
}

The output below will logically not make sense at first glance. This is because the values used above exceeded the precision of a float.

1,222,355.38
1,222,355.63
12,223,551.00
12,223,552.00

Convert using DecimalFormat

A utility class named java.text.DecimalFormat can be used as an alternative way for converting float to String using two decimal places. Below is a simple example:

/**
* A simple Java example that converts a float to String with two decimal places using DecimalFormat.
*/
public class Test {
public static void main(String[] args) {
float number = 123.1465f;
DecimalFormat decimalFormat = new DecimalFormat("#.00");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
}
}

The “.00” in the parameter tells the formatter to use two decimal places while the “#” means to display the whole number as it is. The output is also rounded, which should be:

123.15

Similarly, DecimalFormat also supports thousand separator, or a comma for every three digits. Below is an example on how to do that:

/**
* A simple Java example that converts a float to String with two decimal places and using comma
* as thousand separator via DecimalFormat.

*/
public class Test {
public static void main(String[] args) {
float number = 12223.3655f;
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
String numberAsString = decimalFormat.format(number);
System.out.println(numberAsString);
}
}

Whole numbers are comma separated every three digit and the decimal place is rounded to two decimal. Hence the output is:

12223.37

Similar to above approach, be very careful of erratic behavior of very large float precision. Consider using double instead. Here is an example of the problem:

/**
* A simple Java example that converts a very large float to String using DecimalFormat.
*/
public class Test {
public static void main(String[] args) {
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
System.out.println(decimalFormat.format(1222355.3655f));
System.out.println(decimalFormat.format(1222355.6585f));
System.out.println(decimalFormat.format(12223551.3655f));
System.out.println(decimalFormat.format(12223551.6585f));
}
}

And the output is similar, which logically does not make sense at first glance. Because the rounding logic seems incorrect. But the problem actually is with the supported precision of a float number.

1,222,355.38
1,222,355.62
12,223,551.00
12,223,552.00