把数字转换成中文货币表示
以下是一个把BigDecimal转化成中文的货币表示的一段代码,博君一哂!
//中文金额单位数组
straChineseUnit = new String[] {"分", "角", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};
//中文数字字符数组
straChineseNumber = new String[] {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
/**
* Description 将数字金额转换为中文金额
* @param
* @return String
调用:myToChineseCurrency("101.89")="壹佰零壹圆捌角玖分"
myToChineseCurrency("100.89")="壹佰零捌角玖分"
myToChineseCurrency("100")="壹佰圆整"
*/
public String DoNumberCurrencyToChineseCurrency(BigDecimal bigdMoneyNumber) {
String strChineseCurrency = "";
//零数位标记
boolean bZero = true;
//中文金额单位下标
int ChineseUnitIndex = 0;
DoPrintMessage("Calling Method DoNumberCurrencyToChineseCurrency");
try {
if (bigdMoneyNumber.intValue() == 0)
return "零圆整";
//处理小数部分,四舍五入
double doubMoneyNumber = Math.round(bigdMoneyNumber.doubleValue() * 100);
//是否负数
boolean bNegative = doubMoneyNumber < 0;
//取绝对值
doubMoneyNumber = Math.abs(doubMoneyNumber);
//循环处理转换操作
while (doubMoneyNumber > 0) {
//整的处理(无小数位)
if (ChineseUnitIndex == 2 && strChineseCurrency.length() == 0)
strChineseCurrency = strChineseCurrency + "整";
//非零数位的处理
if (doubMoneyNumber % 10 > 0) {
strChineseCurrency = straChineseNumber[(int)doubMoneyNumber % 10] + straChineseUnit[ChineseUnitIndex] + strChineseCurrency;
bZero = false;
}
//零数位的处理
else {
//元的处理(个位)
if (ChineseUnitIndex == 2 ) {
//段中有数字
if (doubMoneyNumber > 0) {
strChineseCurrency = straChineseUnit[ChineseUnitIndex] + strChineseCurrency;
bZero = true;
}
}
//万、亿数位的处理
else if (ChineseUnitIndex == 6 || ChineseUnitIndex == 10) {
//段中有数字
if (doubMoneyNumber % 1000 > 0)
strChineseCurrency = straChineseUnit[ChineseUnitIndex] + strChineseCurrency;
}
//前一数位非零的处理
if (!bZero)
strChineseCurrency = straChineseNumber[0] + strChineseCurrency;
bZero = true;
}
doubMoneyNumber = Math.floor(doubMoneyNumber / 10);
ChineseUnitIndex ++;
}
//负数的处理
if (bNegative)
strChineseCurrency = "负" + strChineseCurrency;
}
catch (Exception e) {
DoPrintMessage("Exception Reason : Call Method DoNumberCurrencyToChineseCurrency Error");
DoPrintMessage("Exception Message : " + e.getMessage());
e.printStackTrace();
return "";
}
return strChineseCurrency;
}
以下是一个把BigDecimal转化成中文的货币表示的一段代码,博君一哂!
//中文金额单位数组
straChineseUnit = new String[] {"分", "角", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};
//中文数字字符数组
straChineseNumber = new String[] {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
/**
* Description 将数字金额转换为中文金额
* @param
BigDecimal bigdMoneyNumber 转换前的数字金额
* @return String
调用:myToChineseCurrency("101.89")="壹佰零壹圆捌角玖分"
myToChineseCurrency("100.89")="壹佰零捌角玖分"
myToChineseCurrency("100")="壹佰圆整"
*/
public String DoNumberCurrencyToChineseCurrency(BigDecimal bigdMoneyNumber) {
String strChineseCurrency = "";
//零数位标记
boolean bZero = true;
//中文金额单位下标
int ChineseUnitIndex = 0;
DoPrintMessage("Calling Method DoNumberCurrencyToChineseCurrency");
try {
if (bigdMoneyNumber.intValue() == 0)
return "零圆整";
//处理小数部分,四舍五入
double doubMoneyNumber = Math.round(bigdMoneyNumber.doubleValue() * 100);
//是否负数
boolean bNegative = doubMoneyNumber < 0;
//取绝对值
doubMoneyNumber = Math.abs(doubMoneyNumber);
//循环处理转换操作
while (doubMoneyNumber > 0) {
//整的处理(无小数位)
if (ChineseUnitIndex == 2 && strChineseCurrency.length() == 0)
strChineseCurrency = strChineseCurrency + "整";
//非零数位的处理
if (doubMoneyNumber % 10 > 0) {
strChineseCurrency = straChineseNumber[(int)doubMoneyNumber % 10] + straChineseUnit[ChineseUnitIndex] + strChineseCurrency;
bZero = false;
}
//零数位的处理
else {
//元的处理(个位)
if (ChineseUnitIndex == 2 ) {
//段中有数字
if (doubMoneyNumber > 0) {
strChineseCurrency = straChineseUnit[ChineseUnitIndex] + strChineseCurrency;
bZero = true;
}
}
//万、亿数位的处理
else if (ChineseUnitIndex == 6 || ChineseUnitIndex == 10) {
//段中有数字
if (doubMoneyNumber % 1000 > 0)
strChineseCurrency = straChineseUnit[ChineseUnitIndex] + strChineseCurrency;
}
//前一数位非零的处理
if (!bZero)
strChineseCurrency = straChineseNumber[0] + strChineseCurrency;
bZero = true;
}
doubMoneyNumber = Math.floor(doubMoneyNumber / 10);
ChineseUnitIndex ++;
}
//负数的处理
if (bNegative)
strChineseCurrency = "负" + strChineseCurrency;
}
catch (Exception e) {
DoPrintMessage("Exception Reason : Call Method DoNumberCurrencyToChineseCurrency Error");
DoPrintMessage("Exception Message : " + e.getMessage());
e.printStackTrace();
return "";
}
return strChineseCurrency;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-130835/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/374079/viewspace-130835/