How to Round Double and Float up to Two Decimal Places in Java?
In Java, when we use a double data type before a variable it represents 15 digits after the decimal point. But sometimes we require only two decimal places after decimal points like to represent rupees and other units. So, in this section, we are going to discuss the ways to round double and float up to two decimal places along with a proper example and Java programs.
Java provides the following three ways to display double in 2 decimal places:
Let's discuss the above ways one by one.
Java DecimalFormat is a concrete subclass of the NumberFormat class that is used to format the decimal numbers. the class provides the setRoundingMode() method to display a double number up to two decimal places.
It accepts rounding mode as a parameter and overrides the setRoundingMode() method of the NumberFormat class. It throws NullPointerException if we do not specify any roundingMode. The following table describes the different rounding modes.
We have used another method of the class DecimalFormat is the format() method. It formats a string to produce a string.
The method accepts three parameters number, result, and position. But the last two parameters are optional. It returns the formatted number string. The method throws ArithmeticException if RoundingMode is set to UNNECESSARY.
Double Number: 123.9876543567 Double Number: 123.99 Before Rounding: 123.98 After Rounding Up: 123.99
Using String.format() Method
Java also provides the format() method to format the number. It belongs to the String class. By using the method, one can format any number and string accordingly.
In order to alter the number up to two decimal places, we use %.2f to format the number. Note that we cannot round the number in String.format() method, always rounding half-up.
The method accepts the following two parameters:
format: It is a formatted string that we want.
args: These are the arguments referenced by the format specifiers in the format string.
It returns the formatted string. It throws IllegalFormatException if the format string contains illegal syntax. It also throws NullPointerException if we specify the format as null.
Double Number: 10.98765432167 Double Number: 10.99 Double Number: 10.99
We can also use Java BigDecimal Class for displaying a number up to two decimal places. It belongs to java.math.BigDecimal package. It extends the Number class and implements the Comparable<BigDecimal> interface.
The class provides the setScale() method. The syntax is as follows:
The method accepts two parameters:
newScale: The scale of the BigDecimal value to be returned.
roundingMode: The rounding mode that we want to apply.
It returns the BigDecimal whose scale is the specified value and whose unscaled value is determined by multiplying or dividing this BigDecimal's unscaled value by the appropriate power of ten to maintain its overall value.
The method throws the ArithmeticException if RoundingMode is set to UNNECESSARY. The RoundingMode is an enum that provides the RoundingMode discussed above.
Another method that we have used in this program is BigDecimal.doubleValue(). It converts BigDecimal to a double number. Let's see an example.
Double Number: 12.4565652239 Up to two decimal places: 12.46