java print double value in 2 decimal points

www.‮l‬autturi.com
java print double value in 2 decimal points
/**
 * @author lautturi.com
 * Java example: print double in fixed decimal
 */

import java.text.DecimalFormat;

public class Lautturi {

	public static void main(String[] args) {

		DecimalFormat df = new DecimalFormat("0.00");
		double pi = 3.1415926;
		System.out.println("double : " + pi);
        System.out.println("double : " + df.format(pi));    //3.14

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

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

	}
}

output:

double : 3.1415926
double : 3.14

double (RoundingMode.DOWN) : 3.14
double (RoundingMode.UP)  : 3.15
Created Time:2017-09-23 00:00:03  Author:lautturi