财务数字格式化
来源:互联网 发布:鸿星尔克官方淘宝店 编辑:程序博客网 时间:2024/04/29 06:49
在财务统计上经常对小数点进行操作,无论是采用平衡价全法还是后进先出都会遇上小数,但是方式大体分为三种。示例如下
/**
* @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();
}
}
* @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();
}
}
- 财务数字格式化
- JS在用户输入的时候格式化数字为财务数字,如"123,123,1.123,1"
- 大写中文数字-财务
- [EXTNET]之文本财务格式化
- 格式化成财务类型数字
- 财务数字转换--大小写转换
- 财务系统数字转化方法
- 财务化显示textfield数字
- 数字格式化
- 数字格式化
- 数字格式化
- 数字格式化
- 格式化 数字
- 数字格式化
- 格式化数字
- 格式化数字
- 数字格式化
- 格式化数字
- 同步(synchronized)对程序性能的影响!
- 关于无线网络
- GridView点击任意地方选中
- 关于FeedBurner及未备案BBS之被封
- 各个网络端口的入侵方法
- 财务数字格式化
- 500个QQ号
- 详细解说STL string
- pligg9.7beta搭起后HTML编辑器插件的安装——fckeditor的配置与修改变相支持中文文件名:默认上传后不能正常调用
- 酒吧Club
- 使用 CInternetSession 封装多线程 http 文件下载
- 四年级数学题难倒家长(转载)
- 缓冲区溢出攻防
- 用多路复用I/O模型实现支持多个客户端的通信服务