/*
功能:四舍五入运算
输入:double value 原数据
int n 保留小数位数
返回:四舍五入后的结果double
*/
public static double roundHalf(double value,int n){
BigDecimal bDec = new BigDecimal(value);
if (n<0) n=0;
bDec = bDec.setScale(n,BigDecimal.ROUND_HALF_EVEN);//四舍五入
value=bDec.doubleValue();
return value;
}/**
* 千分位格式化数据
* @param str String
* @return String
*/
public static String formatDec(String str){
int iPoint=str.indexOf(".");
int iLen=str.length();
String temp="";
if(iLen<4)
return str;
if(iPoint<0)
iPoint=0;
else
iLen=iPoint;
for(int i=3;i<iLen;i=i+3){
temp=str.substring(iLen-i);
str=str.substring(0,iLen-i)+",";
str+=temp;
i++;
iLen++;
}
return str;
} /**
* 取消千分位格式化,返回实际值,如123,12.00 应返回12312.00
* @param str String
* @return String
*/
public static String unFormatDec(String str){
str=str.replaceAll(",","");
return str;
}
功能:四舍五入运算
输入:double value 原数据
int n 保留小数位数
返回:四舍五入后的结果double
*/
public static double roundHalf(double value,int n){
BigDecimal bDec = new BigDecimal(value);
if (n<0) n=0;
bDec = bDec.setScale(n,BigDecimal.ROUND_HALF_EVEN);//四舍五入
value=bDec.doubleValue();
return value;
}/**
* 千分位格式化数据
* @param str String
* @return String
*/
public static String formatDec(String str){
int iPoint=str.indexOf(".");
int iLen=str.length();
String temp="";
if(iLen<4)
return str;
if(iPoint<0)
iPoint=0;
else
iLen=iPoint;
for(int i=3;i<iLen;i=i+3){
temp=str.substring(iLen-i);
str=str.substring(0,iLen-i)+",";
str+=temp;
i++;
iLen++;
}
return str;
} /**
* 取消千分位格式化,返回实际值,如123,12.00 应返回12312.00
* @param str String
* @return String
*/
public static String unFormatDec(String str){
str=str.replaceAll(",","");
return str;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货