Main Tutorials

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

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
mostafa
4 years ago

God bless you man 😀

Nick
4 years ago

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

Martin
2 years ago
Reply to  Nick

Just use String.format(“%.2f”, 7.00);

Nick
4 years ago
Reply to  Nick

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

aboudi
5 years ago

thank you…
that was very helpful.

KuchiMonster
7 years ago

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?

ALIREZA MAJIDI
2 years ago

great, tnx

Arantz
2 years ago

It even works with JOptionPane! Thank you!

Aune khan
2 years ago

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

Fazlay_Rabbi
3 years ago

Method 2 works like a charm! Thanks man.

Adrian
3 years ago

thank you

Sumit
3 years ago

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

Nadar0wsky
2 years ago
Reply to  Sumit

change #.## to #.00

Vaishnavi Yerpude
4 years ago

Thanks .it worked

Mustafa dev
4 years ago

cool

ajay katoch
8 years ago

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

Dmitry Fucintv
8 years ago

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?

chiefrocker86
8 years ago

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

}

}