我让项目将厘米转换为英寸。 我做到了: 我如何使用 Math.round 对数字进行四舍五入?
import java.util.Scanner;
public class Centimer_Inch
{
public static void main (String[] args)
{
// 2.54cm is 1 inch
Scanner cm = new Scanner(System.in); //Get INPUT from pc-Keyboard
System.out.println("Enter the CM:"); // Write input
//double
double centimeters = cm.nextDouble();
double inches = centimeters/2.54;
System.out.println(inches + " Inch Is " + centimeters + " centimeters");
}
}
最佳答案
你可以这样做:
Double.valueOf(new DecimalFormat("#.##").format(
centimeters))); // 2 decimal-places
如果你真的想要Math.round
:
(double)Math.round(centimeters * 100) / 100 // 2 decimal-places
使用 1000
可以保留 3 位小数,使用 10000
可以保留 4 位小数,等等。我个人更喜欢第一个选项。
关于java - 数学轮java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210491/