BigDecimal为不可变的、任意精度的有符号十进制数,其值为(unscaledValue * 10-scale)其中,unscaledValue(非标度值)为任意精度的整数、scale(标度)为32位整型(可为负)
在财务统计上经常对小数点进行操作,无论是采用平衡价全法还是后进先出都会遇上小数,但是方式大体分为三种。示例如下
/**
* @param aStrNumeric---传入的数字字符串
* @param IntDecimalLen---要保留的小数位数
* @param aIntRoundStatus---采用的计算模式 0,1 ,2, other
* @return String type of formatted aStrNumeric
*/
public static String roundNumeric(String aStrNumeric, int aIntDecimalLen, int aIntRoundStatus)
{ //处理空对象或空串
if(aStrNumeric == null || aStrNumeric.length() == 0)
{
return "";
}
//非正数处理,还原为零
if(aIntDecimalLen < 0)
{
aIntDecimalLen = 0;
}
String strNumericArr[] = aStrNumeric.split("/.");
String strNumeric = strNumericArr[0];
System.out.println(strNumeric);
if(strNumericArr.length == 2)
{
if(strNumericArr[1].length() > aIntDecimalLen)
{
strNumeric = (new StringBuilder()).append(strNumeric).append(".").append(strNumericArr[1].substring(0, aIntDecimalLen + 1)).toString();
BigDecimal bdNumeric = new BigDecimal(strNumeric);
System.out.println(strNumeric);
switch(aIntRoundStatus)
{
//ROUND_HALF_UP等于4,此为执行四舍五入。
case 0:
strNumeric = bdNumeric.divide(new BigDecimal("1"), aIntDecimalLen, 4).toString();
break;
//ROUND_UP等于0,此始终执行远离零的方式,终不会减少计算值的大小。
case 1:
strNumeric = bdNumeric.divide(new BigDecimal("1"), aIntDecimalLen, 0).toString();
break;
//ROUND_DOWN等于1,接近零的舍入模式,始终不会增加计算值的大小。
case 2:
strNumeric = bdNumeric.divide(new BigDecimal("1"), aIntDecimalLen, 1).toString();
break;
//默认不执行操作
default:
strNumeric = (new StringBuilder()).append(strNumeric).toString();
break;
}
} else
{
strNumeric = (new StringBuilder()).append(strNumeric).append(".").append(strNumericArr[1]).toString();
}
}
return strNumeric;
}
/**
* @param aStrNumeric---传入的数字字符串
* @param IntDecimalLen---要保留的小数位数
* @param aIntRoundStatus---采用的计算模式 0,1,2, other
* @param bolDecimalZeroStatus---对要求的位数是否做补零处理,true:补零处理,false:不做补零处理
* @return String type of formatted aStrNumeric
*/
public static String formatNumeric(String aStrNumeric, int aIntDecimalLen, int aIntRoundStatus, boolean bolDecimalZeroStatus)
{
StringBuffer strBufRet = new StringBuffer();
String strNumeric = roundNumeric(unformatNumeric(aStrNumeric), aIntDecimalLen, aIntRoundStatus);
if(strNumeric == null || strNumeric.length() == 0)
{
return "";
}
String strNumericArr[] = strNumeric.split("/.");
String strMark = "";
String strInteger = strNumericArr[0];
//对为负数的情况进行处理
if("-".equals(strInteger.substring(0, 1)))
{
strMark = "-";
strInteger = strInteger.substring(1);
}
strBufRet.append(strMark);
strBufRet.append(addComma(strInteger));
String strDecimal = "";
if(strNumericArr.length == 2)
{
strDecimal = strNumericArr[1];
//对尾数进行处理
if(!bolDecimalZeroStatus)
{
strDecimal = trimZeros(strDecimal, 1);
} else
{
int intLen = strDecimal.length();
for(int i = intLen; i < aIntDecimalLen; i++)
{
strDecimal = (new StringBuilder()).append(strDecimal).append("0").toString();
}
}
} else
if(aIntDecimalLen > 0 && bolDecimalZeroStatus)
{
for(int i = 0; i < aIntDecimalLen; i++)
{
strDecimal = (new StringBuilder()).append(strDecimal).append("0").toString();
}
}
if(!"".equals(strDecimal))
{
strBufRet.append(".");
strBufRet.append(strDecimal);
}
return strBufRet.toString();
}
private static String addComma(String aStrInteger)
{
StringBuffer strBufRet = new StringBuffer();
int intLen = aStrInteger.length();
if(intLen <= 3)
{
return aStrInteger;
} else
{
strBufRet.append(addComma(aStrInteger.substring(0, intLen - 3)));
strBufRet.append(",");
strBufRet.append(aStrInteger.substring(intLen - 3));
return strBufRet.toString();
}
}