In Java, you can round double
and float
numbers to two decimal places using several different approaches. Let’s see some java programs to see how can we achieve this.
1. Using Math.round
We already covered this method in detail at: Java Math.round() method. Since, we are dealing with two decimal places, you can simply multiply the number by 100, round it to the nearest integer, and then divide by 100. This method works for both double
and float
numbers.
public class RoundExample {
public static void main(String[] args) {
double doubleValue = 143.456789; //double
float floatValue = 143.456789f; //float
double roundedDouble = Math.round(doubleValue * 100.0) / 100.0;
float roundedFloat = Math.round(floatValue * 100.0f) / 100.0f;
// Output: 143.46
System.out.println("Rounded double: " + roundedDouble);
// Output: 143.46
System.out.println("Rounded float: " + roundedFloat);
}
}
2. Using BigDecimal
Another wonderful method to round double and float to two decimal places is using BigDecimal
. In this approach, there are two main steps:
1. Convert double/float
to BigDecimal
:
//double to BigDecimal
BigDecimal bdDouble = new BigDecimal(Double.toString(doubleValue));
//float to BigDecimal
BigDecimal bdFloat = new BigDecimal(Float.toString(floatValue));
First step is to convert the given double and float values to BigDecimal. This can be done using Double.toString()
method.
2. Set Scale and Rounding Mode:
bdDouble = bdDouble.setScale(2, RoundingMode.HALF_UP);
bdFloat = bdFloat.setScale(2, RoundingMode.HALF_UP);
setScale(2, RoundingMode.HALF_UP)
sets the scale of the BigDecimal
to 2 decimal places.RoundingMode.HALF_UP
is the rounding mode that rounds towards “nearest neighbour” unless both neighbours are at equal distance, in which case it rounds up. For example, 2.555 becomes 2.56, and 2.554 becomes 2.55.
Let’s see the complete program:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class RoundExample {
public static void main(String[] args) {
double doubleValue = 143.456789;
float floatValue = 143.456789f;
// Convert double to BigDecimal
BigDecimal bdDouble = new BigDecimal(Double.toString(doubleValue));
bdDouble = bdDouble.setScale(2, RoundingMode.HALF_UP);
// Convert float to BigDecimal
BigDecimal bdFloat = new BigDecimal(Float.toString(floatValue));
bdFloat = bdFloat.setScale(2, RoundingMode.HALF_UP);
// Output: 143.46
System.out.println("Rounded double: " + bdDouble.doubleValue());
// Output: 143.46
System.out.println("Rounded float: " + bdFloat.floatValue());
}
}
3. Using String.format
Let’s see the third approach to round a number to two decimal places. In this method, we will use String.format
method. In the following program, In the format specifier %.2f
, the f indicates decimal while point (.2
) indicates the two decimal places. This method returns a String
representation of the rounded number.
public class RoundExample {
public static void main(String[] args) {
double doubleValue = 143.456789;
float floatValue = 143.456789f;
String roundedDouble = String.format("%.2f", doubleValue);
String roundedFloat = String.format("%.2f", floatValue);
// Output: 143.46
System.out.println("Rounded double: " + roundedDouble);
// Output: 143.46
System.out.println("Rounded float: " + roundedFloat);
}
}
4. Using DecimalFormat
In this approach, we create an instance of DecimalFormat
with the pattern #.00
. The pattern #.00
indicates that the number should have at least one digit before the decimal point and exactly two digits after the decimal point.
import java.text.DecimalFormat;
public class RoundExample {
public static void main(String[] args) {
double doubleValue = 143.456789;
float floatValue = 143.456789f;
DecimalFormat df = new DecimalFormat("#.00");
String roundedDouble = df.format(doubleValue);
String roundedFloat = df.format(floatValue);
// Output: 143.46
System.out.println("Rounded double: " + roundedDouble);
// Output: 143.46
System.out.println("Rounded float: " + roundedFloat);
}
}
Leave a Reply