写的有点着急,应该还有很多优化的空间。(这个是直接对一个数字进行对应位数的进一处理)
/**
* 进一法
* @param num
* @param xiaoshu 保留位数
* @return
*/
public Double jinYi(Double num,Integer xiaoshu){
Double newNum = num*Math.pow(10, xiaoshu);
if(Math.abs(newNum-Math.round(newNum))>0){//判断是否需要进一
String strNum = String.valueOf((newNum+1)/Math.pow(10, xiaoshu));
if(strNum.indexOf(“.”) > 0){
return Double.parseDouble(strNum.substring(0, (strNum.indexOf(“.”)+xiaoshu+1)));
}else{
return Double.parseDouble(strNum);
}
}else{
return num;
}
}