java double 2 decimal

java double 2 decimal
/**
 * @author lautturi.com
 * Java example: round a double to 2 decimal places
 */
import java.util.*;
import java.text.DecimalFormat;

public class Lautturi {

	public static void main(String[] args) {

		double test = 3.14159;
		DecimalFormat df = new DecimalFormat("#.0");
		System.out.println(df.format(test)); // Console: 12.2
		// # - prints a digit if provided, nothing otherwise
		// . - indicates where to put the decimal seperator
		// 0 - prints a digit if provided, 0 otherwise
		
	}
}
Source:‮w‬ww.lautturi.com

output:

3.1
Created Time:2017-09-18 14:34:45  Author:lautturi