# Java – How to round double / float value to 2 decimal points

In Java, there are a few ways to round `float` or `double` to 2 decimal places.

Note

## 1. DecimalFormat

DecimalExample.java
``````
package com.mkyong;

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

public class DecimalExample {

private static 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

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

## 2. BigDecimal

BigDecimalExample.java
``````
package com.mkyong;

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

public class BigDecimalExample {

public static void main(String[] args) {

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

BigDecimal bd = new BigDecimal(input).setScale(2, RoundingMode.HALF_UP);
double salary = bd.doubleValue();

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

}

}
``````

Output

``````
salary : 1205.6358
salary : 1205.64
``````

## 3. Math.round

MathExample.java
``````
package com.mkyong;

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

``````
salary : 1205.6358
salary : 1205.64
``````

Explanation, just for academy purpose, for 3 decimal places, `*1000`

``````
input = 1205.6358;

Math.round(input * 100.0) / 100.0;

Math.round(120563.58) / 100.0;

120564 / 100.0;

salary = 1205.64
``````

## References ##### mkyong
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. 0 Followers

Most reacted comment
14 Comment authors     Recent comment authors Guest
Danny

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 … ? Guest
Biggles

Why use custom code here?

It is faster
It is more concise
It returns a double instead of a string

Built in doesnt always mean better. Guest
Ankit

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)); Guest
Tina

Output for 2. BigDecimal is incorrect.
Expected result:
double : 3.14159265359
salary : 3.14 Guest
frans

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()); Guest
Sajith Keragala Guest
shubh

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 Guest
isabella

man, thank you so much.

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

Keep up with the good work, bye. Guest
borygo88

thanks for that tip Guest
Thirsty

millions of Thanks Guest
Eduardo

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? Guest
jesse boyd

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. Guest
Ronak Jain

(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. Guest
g00glen00b

I think the first way is obviously not the best way to go. For someone who is reading the code for the first time, you might ask where the magical number 100.0 comes from.
However, if you still need a double/float value that has a certain precision, use the BigDecimal type and change the scale. This is the same thing that happens when you’re using the Apache commons, Precision class: http://grepcode.com/file/repo1.maven.org/maven2/org.apache.commons/commons-math3/3.0/org/apache/commons/math3/util/Precision.java#338 (in the end I just used that class)