首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

double门类四舍五入取两位小数,java中程序怎样写

2011-12-18 
double类型四舍五入取两位小数,java中程序怎样写?doublecost0.299999999998用java程序怎么样才能四舍五

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) ;
}
}

热点排行
Bad Request.