Display double in 2 decimal points in Java
display double
2 decimal
This tutorial will show you how display double in 2 decimal points in Java using String format method and DecimalFormat class.
String format
To display a specific number of decimals you need to use the static method format from String class.
String.format("%.2f", number)
where 2 is the number of decimals.
Example
package com.admfactory;
public class StringFormatExample {
public static void main(String[] args) {
double a1 = 12.344567;
double a2 = 12.767896;
System.out.println("StringFormat Example");
System.out.println("double : " + a1);
System.out.println("double format: " + String.format("%.2f", a1));
System.out.println();
System.out.println("double : " + a2);
System.out.println("double format: " + String.format("%.2f", a2));
}
}
Output
StringFormat Example
double : 12.344567
double format: 12.34
double : 12.767896
double format: 12.77
As you can notice in the output the rounding mode is towards the "nearest neighbour".
DecimalFormat
To setup the number of decimals you need to pass the format in the DecimalFormat constructor. e.g. ".##" where the count of '#' chars is the number of decimals.
By default, DecimalFormat uses for rounding mode HALF_EVEN. If you need to change it can be easily done using the setRoundingMode method
df.setRoundingMode(RoundingMode.UP);
There are a few options in terms of rounding in RoundingMode class.
CEILING
: Rounding mode to round towards positive infinity.DOWN
: Rounding mode to round towards zero.FLOOR
: Rounding mode to round towards negative infinity.HALF_DOWN
: Rounding mode to round towards "nearest neighbour" unless both neighbours are equidistant, in which case round down.HALF_EVEN
: Rounding mode to round towards the "nearest neighbour" unless both neighbours are equidistant, in which case, round towards the even neighbour.HALF_UP
: Rounding mode to round towards "nearest neighbour" unless both neighbours are equidistant, in which case round up.UNNECESSARY
: Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.UP
: Rounding mode to round away from zero.
Example
package com.admfactory;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat(".##");
double a1 = 12.344567;
double a2 = 12.767896;
System.out.println("DecimalFormat Example");
System.out.println("double : " + a1);
System.out.println("double format: " + df.format(a1));
System.out.println();
System.out.println("double : " + a2);
System.out.println("double format: " + df.format(a2));
df.setRoundingMode(RoundingMode.UP);
System.out.println("double format (UP): " + df.format(a2));
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("double format (DOWN): " + df.format(a2));
}
}
Output
DecimalFormat Example
double : 12.344567
double format: 12.34
double : 12.767896
double format: 12.77
double format (UP): 12.77
double format (DOWN): 12.76