import java.math.BigDecimal;
/**
* <p>
* Title: 金额数字转换成中文写法
* </p>
* <p>
* Description: 金额数字转换成中文写法
* </p>
*/
public class ChineseCapital {
static String[] hzUnit = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰",
"仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
static String[] hzNum = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
/***************************************************************************
* 金额转大写(后面加整)
*
* @param Num
* @return
*/
public static String toChineseCapitalMoney(BigDecimal Num) {
Num = Num.multiply(new BigDecimal("100"));
Num=Num.setScale(2);
// Num.movePointRight(2);
String lwCase = Num.toString();
int nPointPos = lwCase.indexOf('.');
if (nPointPos == -1)
nPointPos = lwCase.length();
lwCase = lwCase.substring(0, nPointPos);
lwCase = lwCase.trim();
int nLen = lwCase.length();
int iAddZero = 0;
String szChMoney = "";
String strDigital = "";
int nDigital = 0;
if (nLen > 15 || nLen == 0 || Num.doubleValue() < 0)
return ""; // 数据错误返回
for (int i = 0; i < nLen; i++) {
strDigital = lwCase.substring(i, i + 1);
nDigital = new Integer(strDigital).intValue();
if (nDigital == 0) {
iAddZero++;
} else {
if (iAddZero > 0) {
szChMoney += "零";
}
szChMoney += hzNum[nDigital];
iAddZero = 0;
}
if (nDigital != 0 || nLen - i == 3 || nLen - i == 11
|| ((nLen - i + 1) % 8 == 0 && iAddZero < 4)) // 该位不为0||元位||亿位||万位
szChMoney += hzUnit[nLen - i - 1];
}
if (lwCase.endsWith("0"))
szChMoney += "整";
// if(szNum.Right(2)==_T("00")) szChMoney+=_T("整");
return szChMoney;
}
/***************************************************************************
* 金额转大写(后面不加整)
*
* @param Num
* @return
*/
public static String toChineseCapitalMoney1(BigDecimal Num) {
Num = Num.multiply(new BigDecimal("100"));
Num=Num.setScale(2);
// Num.movePointRight(2);
String lwCase = Num.toString();
int nPointPos = lwCase.indexOf('.');
if (nPointPos == -1)
nPointPos = lwCase.length();
lwCase = lwCase.substring(0, nPointPos);
lwCase = lwCase.trim();
int nLen = lwCase.length();
int iAddZero = 0;
String szChMoney = "";
String strDigital = "";
int nDigital = 0;
if (nLen > 15 || nLen == 0 || Num.doubleValue() < 0)
return ""; // 数据错误返回
for (int i = 0; i < nLen; i++) {
strDigital = lwCase.substring(i, i + 1);
nDigital = new Integer(strDigital).intValue();
if (nDigital == 0) {
iAddZero++;
} else {
if (iAddZero > 0) {
szChMoney += "零";
}
szChMoney += hzNum[nDigital];
iAddZero = 0;
}
if (nDigital != 0 || nLen - i == 3 || nLen - i == 11
|| ((nLen - i + 1) % 8 == 0 && iAddZero < 4)) // 该位不为0||元位||亿位||万位
szChMoney += hzUnit[nLen - i - 1];
}
return szChMoney;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(ChineseCapital
.toChineseCapitalMoney1(new BigDecimal("2")));//贰元
System.out.println(ChineseCapital.toChineseCapitalMoney(new BigDecimal(
"324129.21")));//叁拾贰万肆仟壹佰贰拾玖元贰角壹分
}
}