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

50分求一个简单问题!!java中如何对一个double变量在十位数四舍五入?
例如:  
789           结果为790
1234.20   结果为1230
2               结果为0
8               结果为10

菜鸟非常感谢大家!


------解决方案--------------------
BigDecimal这个类好像可以实现吧,没试过。
------解决方案--------------------
public static double myRound(double d) {
return Math.round(d/10) * 10;
}

------解决方案--------------------
double a;
if(a%10 > =50){
进一位
}
else{
...
}
------解决方案--------------------
private int roundTen(double db) {
int roundedInt = (int) Math.round(db);

if (roundedInt % 10 < 5) {
return (roundedInt / 10) * 10;
}

return (roundedInt / 10) * 10 + 10;

}

提供测试数据如下:
double db1 = 1234.20;
double db2 = 789;
double db3 = 2;
double db4 = 8;
double db5 = 1234.56;

结果如下:
1230
790
0
10
1240

------解决方案--------------------
Math.round(d/10) * 10
------解决方案--------------------
public static int get(float a)
{
return (int)(a/10+0.5)*10;
}

}

结果:
790
1230
0
10

------解决方案--------------------
Math.round(d/10) * 10

不要加int类型强制转换,防溢出
------解决方案--------------------
/**
* 四舍五入
* @param dSource
* @return
*/
public static int getRound(double dSource) {
int iRound;
//BigDecimal的构造函数参数类型是double
BigDecimal deSource = new BigDecimal(dSource);
//deSource.setScale(0,BigDecimal.ROUND_HALF_UP) 返回值类型 BigDecimal
//intValue() 方法将BigDecimal转化为int
iRound = deSource.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
return iRound;
}

}