Format number to 2 decimal places in Java
Quick example to format any type of number - int, long, float, and double to 2 decimal places in Java
DecimalFormat(“0.00”)
import java.text.DecimalFormat;
public class FormatAnyNumber {
private static final DecimalFormat df = new DecimalFormat("0.00");
public static void main(String[] args) {
int num1 = 12345;
long num2 = 12345;
float num3 = 12345.6f;
double num4 = 12345.6789;
System.out.println("int: " + df.format(num1));
System.out.println("long: " + df.format(num2));
System.out.println("float: " + df.format(num3));
System.out.println("double: " + df.format(num4));
}
}
Output
int: 12345.00
long: 12345.00
float: 12345.60
double: 12345.68
Points to note…
- int and long number has no decimal places, appended “00” at the end for 2 decimal places
- float number has only one decimal place, appended “0” at the end for 2 decimal places
- double number has 4 decimal places, rounded to 2 decimal places.
RoundingMode
By default, DecimalFormat use RoundingMode.HALF_EVEN for rounding. You can use other rounding modes as follows:-
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class FormatAnyNumber {
private static final DecimalFormat df = new DecimalFormat("0.00");
public static void main(String[] args) {
double num = 12345.6789;
df.setRoundingMode(RoundingMode.UP);
System.out.println("double rounding up : " + df.format(num));
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("double rounding down: " + df.format(num));
}
}
Output
double rounding up : 12345.68
double rounding down: 12345.67
String.format("%.2f", num)
String.format (or the equivalent System.out.printf) is a lighter-weight alternative when you don’t need a reusable formatter object — no DecimalFormat instance to construct, just a format string:
double num4 = 12345.6789;
double neg = -12345.6789;
System.out.println(String.format("%.2f", num4)); // 12345.68
System.out.println(String.format("%.2f", neg)); // -12345.68
System.out.printf("%.2f%n", num4); // 12345.68
%.2f rounds using RoundingMode.HALF_UP, not DecimalFormat’s default HALF_EVEN — the two can disagree on numbers that sit exactly halfway between two roundings, so don’t assume they’re interchangeable if your rounding rule matters (e.g. for money).
Locale Pitfall
Both DecimalFormat and String.format’s %f/%.2f conversions use the JVM’s default locale for the decimal separator — that includes String.format, which is easy to forget since %.2f looks purely numeric. On a machine (or a JVM started with -Duser.language=de) whose default locale is German, the exact same code produces a comma instead of a dot:
Locale.setDefault(Locale.GERMANY);
System.out.println(new DecimalFormat("0.00").format(12345.6789)); // 12345,68
System.out.println(String.format("%.2f", 12345.6789)); // 12345,68
If the output is meant for machine consumption (a JSON field, a CSV column, an API response) rather than display to a user, force a fixed locale explicitly — new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.US)) or String.format(Locale.US, "%.2f", num4) — so it doesn’t silently break in a different default locale.
Floating-Point Rounding Trap
Rounding to 2 decimal places can look inconsistent for values that seem symmetric, because double can’t represent most decimal fractions exactly:
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(2.345)); // 2.35
System.out.println(df.format(2.335)); // 2.33
Both look like they should round the same way, but 2.335 rounds down while 2.345 rounds up. That’s not a DecimalFormat bug — it’s because 2.335 cannot be stored exactly as a double; its actual stored value is 2.33499999999999996447..., which is genuinely less than 2.335 and correctly rounds down. 2.345 happens to be stored as 2.34500000000000019..., just above 2.345, so it correctly rounds up. If you need decimal rounding that matches the decimal digits you typed rather than the binary value the double actually holds, construct the value from a String via BigDecimal (e.g. new BigDecimal("2.335").setScale(2, RoundingMode.HALF_UP)) instead of formatting a double literal directly.