Java – Display double in 2 decimal places

In Java, there are few ways to display double in 2 decimal places.

Table of contents.

1. DecimalFormat(“0.00”)

We can use DecimalFormat("0.00") to ensure the number is round to 2 decimal places.

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 = 3.14159265359;

        System.out.println("double : " + input);
        System.out.println("double : " + df.format(input));    //3.14

        // DecimalFormat, default is RoundingMode.HALF_EVEN
        df.setRoundingMode(RoundingMode.DOWN);
        System.out.println("\ndouble (RoundingMode.DOWN) : " + df.format(input));  //3.14

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

    }

}

Output

Terminal

double : 3.14159265359
double : 3.14

double (RoundingMode.DOWN) : 3.14
double (RoundingMode.UP)  : 3.15

Note

You may interest at this article – DecimalFormat("#.##") or DecimalFormat("0.00")

2. String.format(“%.2f”)

We also can use String formater %2f to round the double to 2 decimal places. However, we can’t configure the rounding mode in String.format, always rounding half-up.

StringFormatExample.java

package com.mkyong.math.rounding;

public class StringFormatExample {

  public static void main(String[] args) {

      double input = 3.14159265359;
      System.out.println("double : " + input);
      System.out.println("double : " + String.format("%.2f", input));
      System.out.format("double : %.2f", input);

  }

}

Output

Terminal

double : 3.14159265359
double : 3.14
double : 3.14

3. BigDecimal

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;
      System.out.println("double : " + input);

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

      System.out.println("double : " + newInput);

  }
}

Output

Terminal

double : 3.14159265359
double : 3.14

4. References

23 comments on “Java – Display double in 2 decimal places

  1. Best Tutorial ever. Thank you somuch.. even stack overflow is outdated

    Reply
  2. when you change the value to 1.5 it returns 1.5, I want it to return 1.50.What should I do?

    Reply
  3. I realize that when comes to 7.00 and it become 7. But I want it as 7.00. Any Idea?

    Reply
    1. I fixed it, just simply change from “#.##” to “#.00”

      Reply
  4. When using the String.format example do you know how it rounds? Is it HALF / UP / or DOWN.
    I know it does not have those constants but the formula behind the .format method must round it one of those 3 ways right?

    Reply
  5. Hello,

    Can you share Java code or script for finding P-value of large data sets:-

    eg:-

    Input File (.txt format/or any)

    Name X Y Z

    A_1 1.7085586 0.73179674 3.3962722

    A_2 0.092749596 -0.10030079 -0.47453594

    A_3 1.1727467 0.15784931 0.0572958

    A_4 -0.91714764 -0.62808895 -0.6190882

    A_5 0.34570503 0.10605621 0.30304766

    This should be the Input format in form of text file or Excel sheet

    and the outPut should be:-

    Name p_Value

    A_1 0.129618297992839

    A_2 0.436354399799269

    A_3 0.323631285199105

    A_4 0.0179166576112724

    A_5 0.0763318283011515

    I have done it in R programming, but I want to this by using Java, but I am a beginner in Java so …..

    Any help would be appreciable……..

    Thank you….

    Reply
  6. Hi, mkyong? Can I use code from your examples anyway I want? I mean create my own example based on yours and post it somewhere?

    Reply
  7. What about:

    package com.mkyong.loan;

    public class Test{

    public static void main(String[] args) {

    double input = 32.123456;
    System.out.println(“double : ” + input);
    System.out.printf(“double : %.2fn”, input);

    }

    }

    Reply

Leave a Comment

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