数学圆java [英] Math round java
本文介绍了数学圆java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的项目确实从cm转换为英寸。
我做到了:
如何用Math.round来舍入我的数字?
I got project do convert from cm to inch. I did it: how can i round my number with 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
:
If you really wanted Math.round
:
(double)Math.round(centimeters * 100) / 100 // 2 decimal-places
使用 1000
,您可以使用4个小数位,4 10000
等我个人更喜欢第一个选项。
You can have 3 decimal places by using 1000
, 4 by using 10000
etc. I personally like the first option more.
这篇关于数学圆java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文