Java/Scala/Kotlin: How to round a double (or float) to two decimal places? (formatting output)

Java double FAQ: How do I format Java double and float output to two decimal places, such as when I want to format its output for printing — such as currency — or to display it in a user interface? (Also, how do I do the same thing in Kotlin or Scala?)

Solution

There are at least two ways to round a double or float value to two decimal places in Java:

  • Use the Java DecimalFormat class
  • Use String.format

Note that when you use the DecimalFormat class for showing currency, you’ll really want to use the "#.##" solution that I use below (as opposed to the "0.00" that other websites incorrectly show).

Both decimal-formatting solutions are shown below. Note that these solutions also work with other numbers of decimal positions, such as one decimal position, two decimal positions, three decimal positions, etc.

Solution: Rounding Java numbers with DecimalFormat

Use the Java DecimalFormat class to format double and float values, such as rounding them to two decimal places.

Here’s a double solution:

import java.text.DecimalFormat;
double number = 123.4567;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(number));   // output: 123.46

Note: Different number of decimal positions

If you want to format the output to contain different numbers of decimal places, just change the 0.00 portion of that code as needed. Here are examples showing how to format double values to different numbers of decimal places:

DecimalFormat df1 = new DecimalFormat("0.0");    // 1 position after the decimal
System.out.println(df1.format(number));          // output: 123.5

DecimalFormat df3 = new DecimalFormat("0.000");  // 3 positions after the decimal
System.out.println(df3.format(number));          // output: 123.457

Note: DecimalFormat and "#.##" vs "0.00"

Note that using "0.00" is the correct approach for this problem. If you use "#.##", it will not work properly in the second situation shown here:

DecimalFormat dfPound = new DecimalFormat("#.##");
DecimalFormat dfZero = new DecimalFormat("0.00");

System.out.println(dfPound.format(123.4567));   // 123.46
System.out.println(dfZero.format(123.4567));    // 123.46

System.out.println(dfPound.format(123.4));      // 123.4   (wrong)
System.out.println(dfZero.format(123.4));       // 123.40

Therefore, this is the correct DecimalFormat solution:

DecimalFormat dfZero = new DecimalFormat("0.00");

Solution: Rounding Java numbers with String.format

You can also round Java double and float values using String.format, as shown in this example:

double number = 123.4567;
String formattedNumber = String.format("%.2f", number);
System.out.println(formattedNumber);   // output: 123.46

Discussion: Rounding mode

With DecimalFormat you can also control the “rounding mode,” i.e., whether to round numbers up or down. This is shown in the following examples:

import java.math.RoundingMode;
import java.text.DecimalFormat;

DecimalFormat dfDefault = new DecimalFormat("0.0");

DecimalFormat dfUp = new DecimalFormat("0.0");
dfUp.setRoundingMode(RoundingMode.UP);

DecimalFormat dfDown = new DecimalFormat("0.0");
dfDown.setRoundingMode(RoundingMode.DOWN);

double a = 1.49;
System.out.println("1.49 default: " + dfDefault.format(a));   // 1.5
System.out.println("1.49 up:      " + dfUp.format(a));        // 1.5
System.out.println("1.49 down:    " + dfDown.format(a));      // 1.4

double b = 1.50;
System.out.println("1.50 default: " + dfDefault.format(b));   // 1.5
System.out.println("1.50 up:      " + dfUp.format(b));        // 1.5
System.out.println("1.50 down:    " + dfDown.format(b));      // 1.5

double c = 1.51;
System.out.println("1.51 default: " + dfDefault.format(c));   // 1.5
System.out.println("1.51 up:      " + dfUp.format(c));        // 1.6
System.out.println("1.51 down:    " + dfDown.format(c));      // 1.5

DecimalFormat default rounding mode

The default rounding mode for the Java DecimalFormat class is HALF_EVEN, also known as "rounding mode to nearest neighbor, ties to even". This means that if the number to be rounded is exactly halfway between two others, it will be rounded towards the nearest even number. This rounding mode helps to minimize the bias that can occur when repeatedly rounding a list of numbers.

For example, when rounding the number 0.5 with HALF_EVEN mode, it will be rounded to 0, and when rounding 1.5, it will be rounded to 2.

You can see this demonstrated in the following examples, where I round to zero decimal places:

import java.math.RoundingMode;
import java.text.DecimalFormat;

DecimalFormat dfDefault = new DecimalFormat("0");

DecimalFormat dfUp = new DecimalFormat("0");
dfUp.setRoundingMode(RoundingMode.UP);

DecimalFormat dfDown = new DecimalFormat("0");
dfDown.setRoundingMode(RoundingMode.DOWN);

double a = 1.49;
System.out.println("1.49 default: " + dfDefault.format(a));   // 1
System.out.println("1.49 up:      " + dfUp.format(a));        // 2
System.out.println("1.49 down:    " + dfDown.format(a));      // 1

double b = 1.50;
System.out.println("1.50 default: " + dfDefault.format(b));   // 2
System.out.println("1.50 up:      " + dfUp.format(b));        // 2
System.out.println("1.50 down:    " + dfDown.format(b));      // 1

double c = 1.51;
System.out.println("1.51 default: " + dfDefault.format(c));   // 2
System.out.println("1.51 up:      " + dfUp.format(c));        // 2
System.out.println("1.51 down:    " + dfDown.format(c));      // 1

A few notes

Here are a few notes related to this solution:

  • In the following solutions I always use double values. You can also use float values with the solutions shown, but remember that when you work with float values, there can be precision loss due to the smaller number of bits used to represent float compared to double.
  • At the time of this writing I tested this solution with Java 22, and it also works with Java 8, Java 11, Java 14, Java 17, etc.
  • The same solution works for Scala and Kotlin as well.

Summary

In summary, if you wanted to see solutions for how to round Java double and float values to two decimal places (and other decimal places), including how to round numbers up and down, I hope these examples are helpful.

Note that you can test these solutions online using these “Java online playground” options: