首页 > 解决方案 > 如何使用 Printf

问题描述

如何使用 printf 打印包含需要舍入的多个浮点数的行。我知道我需要使用 "%6.2f" 将其四舍五入到小数点后两位。我需要将变量“temp”和“v”四舍五入到小数点后两位。

我试过这个:

System.out.printf("%6.2f," temp + " " + " 华氏度" + "%6.2f," v + " " + "摄氏度");

这没有用。

System.out.printf(temp + " " + " 华氏度是 " + " " + v + " " + "摄氏度");

标签: javainputprintfoutputprintln

解决方案


这就是你要找的:

System.out.printf("%6.2f degrees Fahrenheit is %6.2f Celsius", temp, v);

推荐阅读