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

double类型四舍五入取两位小数,java中程序怎样写?
double   cost=0.299999999998;
用java程序怎么样才能四舍五入输出
cost=0.30


------解决方案--------------------
10分都懒得进来看。
有专门的方法的。你看看math那各类
------解决方案--------------------

要保留几位就先乘10的几次方,四舍5入再除以10的几次方
------解决方案--------------------

public class Test {
public static void main( String[] argv ) {
double a = 12.36587;
double c0= Math.floor(a *10+.5)/10; // c0 = 12.4
double c1= Math.floor(a *100+.5)/100; // c1 = 12.37
double c2= Math.floor(a *1000+.5)/1000; // c2 = 12.366
double c3= Math.floor(a *10000+.5)/10000; // c3 = 12.3659


System.out.println(c0) ;
System.out.println(c1) ;
System.out.println(c2) ;
System.out.println(c3) ;
}
}