Java examples for Language Basics:Number Format
Using precision for floating-point numbers and strings.
public class Main { public static void main(String[] args) { //from w ww .j a v a 2 s . c om double f = 123.94536; String s = "Happy Birthday"; System.out.printf("Using precision for floating-point numbers\n"); System.out.printf("\t%.3f\n\t%.3e\n\t%.3g\n\n", f, f, f); System.out.printf("Using precision for strings\n"); System.out.printf("\t%.11s\n", s); } }