java round double/float to 2 decimal places

In this post, we will see how to round double to 2 decimal places in java.
There are many ways to do it.Let’s go through few ways.

Let’s understand each with the help of simple example.

Using DecimalFormat

You can use DecimalFormat too to round number to 2 decimal places.

Output:

Rounded double (DecimalFormat) : 2343.55

Using BigDecimal

You can convert double or float to BigDecimal and use setScale() method to round double/float to 2 decimal places.
Here is the example:

Output:

Rounded Double: 2343.55
Rounded Float: 2343.55

Using Math.round(double*100.0)/100.0

Output:

Rounded double: 2343.55
Rounded float: 2343.55

You must be wondering how this works.

double*100.0 – 234354.76
Math.round(double*100.0) – 234355.00 (round to nearest value)
Math.round(double*100.0)/100.0 – 2343.55

Using Apache common Math

You can also use Apache common’s math library to round double or float to 2 decimal places.
Add the following dependency to pom.xml.

You can find versions of commons-math over here.
Here is the example:

Rounded Double: 2343.55
Rounded Float: 2343.55

That’s all about rounding double/float to 2 decimal places

Related Posts

  • 28 September

    PI in Java

    The Pi is a constant value in Mathematics that represents a floating-point value 3.1415. It is mostly used in geometry to calculate area, circumference, volume, etc. If you have studied geometry in your academic, then you may be aware of use of Pi such as calculating area require this constant value. In Java, when we […]

  • 05 October

    Calculate total surface area of Cylinder in java

    In this post, we will see how to calculate total surface area of Cylinder in java. Formula of calculating total surface area of Cylinder is: Surface area of Cylinder = 2 *Π * r * r + 2 *Π * r * h Where r is radius of cylinder and h is height of cylinder […]

  • 05 October

    Calculate total surface area of Hemisphere in java

    In this post, we will see how to calculate total surface area of Hemisphere in java. Hemisphere is exactly half of sphere. There can be many practical examples of Hemisphere. You can divide earth into two hemisphere i.e. Northen Hemisphere and Southern Hemisphere. Formula of calculating total surface area of Hemisphere is: surface area of […]

  • 05 October

    Get random number between 0 and 1 in java

    In this post, we will see how to get random number between 0 to 1 in java. We have already seen random number generator in java. We can simply use Math.random() method to get random number between 0 to 1. Math.random method returns double value between o(inclusive) to 1(exclusive). [crayon-6046923adf0cb029680363/] When you run above program, […]

  • 03 January

    How to get square root of number in java

    In this tutorial. we will see if how to get square root of number in java. It is very simple to get square root of number in java. You can simply use Math’s sqrt() method to calculate square root of number. Syntax [crayon-6046923adf293286785266/] Return type It returns square root of the number. Square root of […]

  • 07 September

    Power function in java

    Power function in java is used to get first argument’s power to 2nd argument. For example: Let’s say you want to calculate 2 to the power 4. That will be 2*2*2*2=16 In java, you can do it by : Math.pow(2,4) =16 Syntax [crayon-6046923adf7d5739611520/] Example : Let’s use power function in java. [crayon-6046923adf7da200977850/] When you run […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.