Main Tutorials

Java – How to round double / float value to 2 decimal places

There are a few ways to round float or double to 2 decimal places in Java.

Table of contents

Note
In short, for monetary calculation, picks BigDecimal; for display purpose, picks DecimalFormat("0.00").

1. DecimalFormat(“0.00”)

We can use DecimalFormat("0.00") to ensure the number always round to 2 decimal places. For DecimalFormat, the default rounding mode is RoundingMode.HALF_EVEN, and we can use setRoundingMode(RoundingMode) to set a specified rounding mode.

DecimalExample.java

package com.mkyong.math.rounding;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalExample {

  private static final DecimalFormat df = new DecimalFormat("0.00");

  public static void main(String[] args) {

      double input = 1205.6358;

      System.out.println("salary : " + input);

      // DecimalFormat, default is RoundingMode.HALF_EVEN
      System.out.println("salary : " + df.format(input));      //1205.64

      df.setRoundingMode(RoundingMode.DOWN);
      System.out.println("salary : " + df.format(input));      //1205.63

      df.setRoundingMode(RoundingMode.UP);
      System.out.println("salary : " + df.format(input));      //1205.64

  }

}

Output

Terminal

salary : 1205.6358
salary : 1205.64
salary : 1205.63
salary : 1205.64

2. DecimalFormat(“0.00”) vs DecimalFormat(“#.##”)

The below shows the difference between DecimalFormat("0.00") and DecimalFormat("#.##").

DecimalExample2.java

package com.mkyong.math.rounding;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalExample2 {

  private static final DecimalFormat dfZero = new DecimalFormat("0.00");
  private static final DecimalFormat dfSharp = new DecimalFormat("#.##");

  public static void main(String[] args) {

      double input = 1205.6;

      System.out.println("salary : " + input);

      System.out.println("salary 0.00 : " + dfZero.format(input));
      System.out.println("salary #.## : " + dfSharp.format(input));

      double input2 = 1205.60;

      System.out.println("salary : " + input2);

      System.out.println("salary 0.00 : " + dfZero.format(input2));
      System.out.println("salary #.## : " + dfSharp.format(input2));


  }

}

Output

Terminal

salary : 1205.6
salary 0.00 : 1205.60
salary #.## : 1205.6

salary : 1205.6
salary 0.00 : 1205.60
salary #.## : 1205.6

DecimalFormat("#.##") displays blank if the second decimal place is empty or zero. The DecimalFormat("0.00") is a better solution for 2 decimal places.

3. BigDecimal

We also can convert the double to a BigDecimal object and set the scale and rounding mode.

BigDecimalExample.java

package com.mkyong.math.rounding;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalExample {

  public static void main(String[] args) {

      //double input = 3.14159265359;
      double input = 1205.6358;
      System.out.println("double : " + input);

      // convert double to BigDecimal
      BigDecimal salary = new BigDecimal(input);
      System.out.println("BigDecimal: " + salary);

      // round to 2 decimal places
      BigDecimal salary2 = salary.setScale(2, RoundingMode.HALF_UP);
      System.out.println("BigDecimal: " + salary2);

      // one line
      BigDecimal salary3 = new BigDecimal(input).setScale(2, RoundingMode.HALF_UP);
      System.out.println("BigDecimal: " + salary3);

      // convert BigDecimal back to double
      double salary4 = salary3.doubleValue();
      System.out.println("double : " + salary4);

  }
}

Output

Terminal

double : 1205.6358
BigDecimal: 1205.63580000000001746229827404022216796875
BigDecimal: 1205.64
BigDecimal: 1205.64
double : 1205.64

4. String.format(“%.2f”, input)

The String.format is working fine, and the default rounding is half-up; however, we have no way to configure the type of rounding mode.

StringFormatExample.java

package com.mkyong.math.rounding;

public class StringFormatExample {

  public static void main(String[] args) {

      double input = 1205.6358;

      System.out.println("salary : " + input);

      // round half-up, no way control
      // 1205.64
      System.out.println("salary : " + String.format("%.2f", input));

      // 1205.64
      System.out.format("salary : %.2f", input);

  }

}

Output

Terminal

salary : 1205.6358
salary : 1205.64
salary : 1205.64

5. Math.round

5.1 This Math.round is for educational purposes 🙂

MathExample.java

package com.mkyong.math.rounding;

public class MathExample {

  public static void main(String[] args) {

      double input = 1205.6358;

      System.out.println("salary : " + input);

      double salary = Math.round(input * 100.0) / 100.0;

      System.out.println("salary : " + salary);

  }

}

Output

Terminal

salary : 1205.6358
salary : 1205.64
Terminal

input = 1205.6358;

Math.round(input * 100.0) / 100.0;

Math.round(120563.58) / 100.0;

120564 / 100.0;

salary = 1205.64

5.2 For 3 decimal places, try * 1000.


  double input = 1205.6358;

  double salary = Math.round(input * 1000.0) / 1000.0;

  System.out.println("salary : " + salary);

Output

Terminal

salary : 1205.6358
salary : 1205.636

6. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
23 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Muhammad Khan
2 years ago

I created an account just to say thank you.

Ronak Jain
9 years ago

(12465,2) than it gives 12500 and (12465,3) than it gives 12000 can any one have idea to write such method.in java
this question was asked to me at interview.

Gabriel
1 year ago

Hi Senior programmer, I am glad you shared your ideas online for us to learn. Well done sir.

Eduardo
10 years ago

First: Math.round(123.50 * 100.0) = 124.0, ok.
Second: Math.round(-123.50 * 100.0) = -123.0!! is this correct?, I wait -124.0.
Isn’t nearest number -124.0 to -123.50?

jesse boyd
6 years ago
Reply to  Eduardo

I had the same issue, Math.round method is overloaded and you are getting the long back make sure you pick the method that returns an int.

Gabriel
1 year ago

please can we do this format like 1,200.00 is yes any ideas to share, please

rdixi
2 years ago

THANKS HELPED OUT!!

Anand Reddy
3 years ago

DecimalFormat df = new DecimalFormat(“0.00”);
double time = 1205.00;    
time = Double.valueOf(df.format(time));
System.out.println(time);
// output 1205.0 but expect 1205.00

Sanjana
1 year ago
Reply to  Anand Reddy

is DecimalFormat class name? Please reply asap

imran
3 years ago

good good

Pavan
3 years ago

Thanks for this. Its useful. Saved sometime. You are hero.

Pradeep Kumar Reddy
3 years ago

In DecimalFormat(“0.00”) or DecimalFormat(“#.##”) ??
In DecimalFormat we should use 0 or # ??

Mahmut Tuncer
4 years ago

Thank you much…DecimalFormat is really good solution.
It is better than Math.Round!

Ankit
6 years ago

int i = 180;
int j = 1;
double div= ((double)(j*100)/i);
DecimalFormat df = new DecimalFormat(“#.00”); // simple way to format till any deciaml points
System.out.println(div);
System.out.println(df.format(div));

frans
7 years ago

EditText GridBase = (EditText) findViewById(R.id.GridBase);
Double GridBasedbl = Double.parseDouble(GridBase.getText().toString());
//
TextView VoltageBase = (TextView) findViewById(R.id.VoltageBase);
Double VoltageBasedbl = Double.parseDouble(VoltageBase.getText().toString());
//
Double product = GridBasedbl*VoltageBasedbl;
Log.d(“Calc product”, Double.toString(product));
EditText GridVoltage = (EditText) findViewById(R.id.GridVoltage);

GridVoltage.setText(String.format(“%.3f”, product));
Log.d(“Calc GridVoltage”, GridVoltage.getText().toString());

Danny
10 years ago

i want to convert -0.00000000758602002145 to zero in two decimal accuracy.

double kilobytes = -0.00000000758602002145;
DecimalFormat df = new DecimalFormat(“###.##”);
System.out.println(“kilobytes (DecimalFormat) : ” + df.format(kilobytes));

the result i got is “-0”

What to do … ?

Sajith Keragala
10 years ago

Please refer that

double lvForeignPremiumAmount=6208.125

System.out.println(“AFT Math.round(lvForeignPremiumAmount*100.0)/100.0 =”+Math.round(lvForeignPremiumAmount*100.0)/100.0);

System.out.println(“AFT formatter.format(lvForeignPremiumAmount)=”+formatter.format(lvForeignPremiumAmount));

Therefor Please advice which method could be more accurate.

shubh
11 years ago

I want to convert value like 5.8987221219522616E-5…want to avoid E value..please let me know if have any idea about it…thanks

isabella
11 years ago
Reply to  shubh

man, thank you so much.

Saved me a lot of time. Most didatic explanation i’ve found.

Keep up with the good work, bye.

borygo88
11 years ago

thanks for that tip

Thirsty
12 years ago

very helpful
millions of Thanks