/**
* @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:www.lautturi.comoutput:
3.1