Java – Display double in 2 decimal places

In Java, there are few ways to display double in 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 df2 = new DecimalFormat("#.##");

    public static void main(String[] args) {

        double input = 3.14159265359;
        System.out.println("double : " + input);
        System.out.println("double : " + df2.format(input));    //3.14

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

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

    }

}

Output


double : 3.14159265359
double : 3.14
double : 3.14
double : 3.15

2. String.format

StringFormatExample.java

package com.mkyong;

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


double : 3.14159265359
double : 3.14
double : 3.14

3. 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 newInput = bd.doubleValue();

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

    }

}

Output


double : 3.14159265359
double : 3.14

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
7 Comment threads
5 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
Mustafa devNickmkyongaboudiKuchiMonster Recent comment authors
newest oldest most voted
KuchiMonster
Guest
KuchiMonster

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?

Dmitry Fucintv
Guest
Dmitry Fucintv

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?

aboudi
Guest
aboudi

thank you…
that was very helpful.

ajay katoch
Guest
ajay katoch

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 …..… Read more »

Mustafa dev
Guest
Mustafa dev

cool

Nick
Guest
Nick

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

Nick
Guest
Nick

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

chiefrocker86
Guest
chiefrocker86

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

}

}