Java – Display double in 2 decimal points

This article shows you a few examples to display a double variable in 2 decimal points format.

1. DecimalFormat example

TestDouble.java

package com.mkyong.test;

import java.text.DecimalFormat;

public class TestDouble{
	
	private static DecimalFormat df2 = new DecimalFormat(".##");
	
	public static void main(String[] args) {
		
		double input = 32.123456;
		System.out.println("double : " + input);
		System.out.println("double : " + df2.format(input));
		
	}
	
}

Output


double : 32.123456
double : 32.12

By default, DecimalFormat uses RoundingMode.HALF_EVEN.

TestDouble.java

package com.mkyong.test;

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

public class TestDouble {

	private static DecimalFormat df2 = new DecimalFormat(".##");

	public static void main(String[] args) {

		double input = 32.123456;
		System.out.println("double : " + input);
		System.out.println("double (default) : " + df2.format(input));

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

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

	}

}

Output


double : 32.123456
double (default) : 32.12
double (UP) : 32.13
double (DOWN) : 32.12

2. String.format example

TestDouble.java

package com.mkyong.loan;

public class Test{
	
	public static void main(String[] args) {

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

Output


double : 32.123456
double : 32.12

References

  1. DecimalFormat JavaDoc
  2. DecimalFormat RoundingMode JavaDoc
  3. String.format, formatter JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
KuchiMonsterajay katochDmitry Fucintvchiefrocker86 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?

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 »

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

}

}