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

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.favtuts.classic;

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

public class RoundDecimal {

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

    public static void main(String[] args) {
        roundDecimalWithDecimalFormat();
    }

    static void roundDecimalWithDecimalFormat() {
        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. DecimalFormat(“0.00”) vs DecimalFormat(“#.##”)

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

DecimalExample2.java

package com.favtuts.classic;

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

public class RoundDecimal {

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

    public static void main(String[] args) {
        roundDecimalWithDecimalFormatDifference();
    }

    static void roundDecimalWithDecimalFormatDifference() {
        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

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.favtuts.classic;

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

public class RoundDecimal {

    public static void main(String[] args) {
        roundDecimalWithBigDecimal();
    }

    static void roundDecimalWithBigDecimal() {
        // 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

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.favtuts.classic;

public class RoundDecimal {

    public static void main(String[] args) {
        roundDecimalWithStringFormat();
    }

    static void roundDecimalWithStringFormat() {
        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

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.favtuts.classic;

public class RoundDecimal {

    public static void main(String[] args) {
        roundDecimalWithMathRound();
    }

    static void roundDecimalWithMathRound() {
        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
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

salary : 1205.6358
salary : 1205.636

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-basic/classic

References

Leave a Reply

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