Javatpoint Logo
Javatpoint Logo

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.

How to Round Double and Float up to Two Decimal Places in Java

Java provides the following three ways to display double in 2 decimal places:

  1. Using DecimalFormat ("0.00")
  2. Using String.format() Method ("%.2f")
  3. Using BigDecimal

Let's discuss the above ways one by one.

Using DecimalFormat

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.

Syntax:

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.

Put Number UP DOWN CEILING FLOOR HALF_UP HALF_DOWN HALF_EVEN UNNECESSARY
5.5 6 5 6 5 6 5 6 throw ArithmeticException
2.5 3 2 3 2 3 2 2 throw ArithmeticException
1.6 2 1 2 1 2 2 2 throw ArithmeticException
1.1 2 1 2 1 1 1 1 throw ArithmeticException
1.0 1 1 1 1 1 1 1 1
-1.0 -1 -1 -1 -1 -1 -1 -1 -1
-1.1 -2 -1 -1 -2 -1 -1 -1 throw ArithmeticException
-1.6 -2 -1 -1 -2 -2 -2 -2 throw ArithmeticException
-2.5 -3 -2 -2 -3 -3 -2 -2 throw ArithmeticException
-5.5 -6 -5 -5 -6 -6 -5 -6 throw ArithmeticException

We have used another method of the class DecimalFormat is the format() method. It formats a string to produce a string.

Syntax:

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.

TwoDecimalPlace1.java

Output:

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.

Syntax:

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.

TwoDecimalPlace2.java

Output:

Double Number: 10.98765432167
Double Number: 10.99
Double Number: 10.99

Using BigDecimal

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:

Syntax:

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.

TwoDecimalPlace3.java

Output:

Double Number: 12.4565652239
Up to two decimal places: 12.46

Next TopicJava 8 Multimap





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA