提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
对浮点数和字符串使用精度。
public class Main { /** * 时 代 J a v a - nowjava.com 提 供 **/ public static void main(String[] args) { 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);/**from 时 代 J a v a 公 众 号**/ } }