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
Read this RoundingMode

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

About the Author

author image
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.

Comments

avatar
11 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
Ankitjesse boydfransRonak Jaing00glen00b Recent comment authors
newest oldest most voted
Biggles
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.

Danny
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 … ?

Ankit
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));

frans
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());

g00glen00b
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)

shubh
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

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

borygo88
Guest
borygo88

thanks for that tip

Thirsty
Guest
Thirsty

very helpful
millions of Thanks

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

Eduardo
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?

jesse boyd
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.

Sajith Keragala
Guest
Sajith Keragala

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.