日期:2014-05-20  浏览次数:20598 次

大姐求助:a除以b 再加0.5 然后四舍五入怎么写这个代码 ?
谢谢!

------解决方案--------------------
Math.round(a) //四舍五入
------解决方案--------------------
Math.round(a/b+0.5)是没问题的,就看你a和b怎么定义的咩
------解决方案--------------------
编译不过?是否能把你的代码给我们看看,就算报错不会连这个原因也找不出来吧?
------解决方案--------------------
Math.round(a/b+0.5)
------解决方案--------------------
Math.round(a/b+0.5)
------解决方案--------------------
Java code
System.out.println(Math.round(24/5+0.5));

------解决方案--------------------
double a=0.5;
double b=5.3;
double c=(a/b+0.5);
double d=Math.round(c) ;
System.out.println(d);
------解决方案--------------------
老大,int不行,返回的类型是double
晕哦,你不知道看看api啊。
------解决方案--------------------
Java code
class Test1
{ 
    public static void main(String[] args)
    { 
        double a=5.2;
        double b=2.3;
        long d=Math.round(a/b+0.5);//Math.round()的参数只能是double型或者float型,返回类型则是long型.
        long c=Math.round((float)a/b+0.5);
        System.out.println(c);
        System.out.println(d);
    } 
}