Limit the number of decimal digits by specifying the precision in Java
Description
The following code shows how to limit the number of decimal digits by specifying the precision.
Example
//from w w w . j ava2 s . c o m
import java.util.Formatter;
public class Main {
public static void main(String[] argv) throws Exception {
Formatter fmt = new Formatter();
fmt.format("Default precision: %f\n", 10.0 / 3.0);
fmt.format("Two decimal digits: %.2f\n\n", 10.0 / 3.0);
System.out.println(fmt);
}
}
The code above generates the following result.