String strLarge = strMoney.substring(0,strMoney.indexOf('万')+1); char[] strLargeUnitChinese = {'万', '拾', '佰', '仟' }; double[] dLargeUnit = { 10000, 100000, 1000000, 10000000 }; char[] charMoney = strLarge.toCharArray(); int nLargeLen = charMoney.length; for (int i = 0 ;i<charMoney.length ; ++i){ if (charMoney[i]=='零') continue; for (int j = 0 ; j<strCountChinese.length ; ++j){ if (charMoney[i] == strCountChinese[j]) dResult[i] = nCount[j]; } for (int j = 0 ; j<strLargeUnitChinese.length ; ++j){ if (charMoney[i] == strLargeUnitChinese[j]) dResult[i] = dLargeUnit[j]; } }
strMoney = strMoney.substring(strMoney.indexOf('万')+1); char[] strUnitChinese = { '分', '角', '圆', '拾', '佰', '仟'}; double[] dUnit = { 0.01,0.1, 1,10, 100, 1000 }; char[] charMoney1 = strMoney.toCharArray(); for (int i = 0 ;i<charMoney1.length ; ++i){ if (i>20) break; if (charMoney1[i]=='零'){ continue; } for (int j = 0 ; j<strCountChinese.length ; ++j){ if (charMoney1[i] == strCountChinese[j]) { dResult[i+nLargeLen] = nCount[j]; }
} for (int j = 0 ; j<strUnitChinese.length ; ++j){ if (charMoney1[i] == strUnitChinese[j]){ dResult[i+nLargeLen] = dUnit[j]; }
} } double Result = 0; for (int i = 0 ; i<dResult.length ; ++i) System.out.println(dResult[i]); for (int i = 0 ; i<dResult.length ; ){ if (dResult[i]==0.0){ ++i; continue; } Result =Result+ dResult[i]*dResult[i+1]; i = i+2; } System.out.println("Result:"+Result);
/**
* 将双精浮点数代表的金额转化中文大写形式
* @param _dMoney 代表双精浮点数的金额
* @return 金额的中文大写形式,如果输入参数<b>dMoney</b>大于10^8或小于0.01返回空串。
*/
public static String toChinese(double _dMoney) {
String[] strArr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
String[] strArr1 = { "分", "角", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
String[] strArr2 = new String[10];
String sRtn = "";
int iTmp;
double dTmp;
try {
_dMoney += 0.001;
if ((_dMoney >= 100000000) || (_dMoney < 0.01)) {
sRtn = "";
} else {
for (int i = 0; i < 10; i++) {
dTmp = _dMoney / Math.pow(10, 7 - i);
iTmp = (new Double(dTmp)).intValue();
_dMoney -= iTmp * Math.pow(10, 7 - i);
if (iTmp != 0) {
strArr2[i] = strArr[iTmp] + strArr1[9 - i];
} else {
strArr2[i] = "";
}
}
boolean bFlag = false;
for (int i = 0; i < 10; i++) {
if (!"".equals(strArr2[i])) {
sRtn += strArr2[i];
bFlag = true;
} else {
if (i == 3) {
sRtn += "万";
bFlag = true;
} else if (i == 7) {
sRtn += "圆";
bFlag = true;
} else if (bFlag) {
sRtn += "零";
bFlag = false;
}
}
}
if (sRtn.startsWith("万")) {
sRtn = sRtn.substring(1, sRtn.length());
}
if (sRtn.startsWith("圆")) {
sRtn = sRtn.substring(1, sRtn.length());
}
while (sRtn.startsWith("零")) {
sRtn = sRtn.substring(1, sRtn.length());
}
if (sRtn.lastIndexOf("零") == (sRtn.length() - 1)) {
sRtn = sRtn.substring(0, sRtn.length() - 1);
}
if (sRtn.startsWith("圆")) {
sRtn = sRtn.substring(1, sRtn.length());
}
iTmp = sRtn.indexOf("圆");
if (iTmp != -1) {
if ("零".equals(sRtn.substring(iTmp - 1, iTmp))) {
sRtn =
sRtn.substring(0, iTmp - 1)
+ sRtn.substring(iTmp, sRtn.length());
}
}
iTmp = sRtn.indexOf("万");
if (iTmp != -1) {
if ("零".equals(sRtn.substring(iTmp - 1, iTmp))) {
sRtn =
sRtn.substring(0, iTmp - 1)
+ sRtn.substring(iTmp, sRtn.length());
}
}
while (sRtn.startsWith("零")) {
sRtn = sRtn.substring(1, sRtn.length());
}
sRtn += "整";
}
} catch (Exception ex) {
}
return sRtn;
}
按位给每个数字一个权值加到总和中去, 并记住最大的权值,
中间会出现 "零" , 也要判断.最后就是 a + b * 10 + c * 10^2 + c *10^3 + 0 + 0 + f*10^6 + ....
strMoney = strMoney.replaceAll("仟万","仟零万");
strMoney = strMoney.replaceAll("佰万","佰零万");
strMoney = strMoney.replaceAll("拾万","拾零万");
char[] strCountChinese = { '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
double[] nCount = { 1,2,3,4,5,6,7,8,9 };
double[] dResult = new double[strMoney.length()];
String strLarge = strMoney.substring(0,strMoney.indexOf('万')+1);
char[] strLargeUnitChinese = {'万', '拾', '佰', '仟' };
double[] dLargeUnit = { 10000, 100000, 1000000, 10000000 };
char[] charMoney = strLarge.toCharArray();
int nLargeLen = charMoney.length;
for (int i = 0 ;i<charMoney.length ; ++i){
if (charMoney[i]=='零')
continue;
for (int j = 0 ; j<strCountChinese.length ; ++j){
if (charMoney[i] == strCountChinese[j])
dResult[i] = nCount[j];
}
for (int j = 0 ; j<strLargeUnitChinese.length ; ++j){
if (charMoney[i] == strLargeUnitChinese[j])
dResult[i] = dLargeUnit[j];
}
}
strMoney = strMoney.substring(strMoney.indexOf('万')+1);
char[] strUnitChinese = { '分', '角', '圆', '拾', '佰', '仟'};
double[] dUnit = { 0.01,0.1, 1,10, 100, 1000 };
char[] charMoney1 = strMoney.toCharArray();
for (int i = 0 ;i<charMoney1.length ; ++i){
if (i>20)
break;
if (charMoney1[i]=='零'){
continue;
} for (int j = 0 ; j<strCountChinese.length ; ++j){
if (charMoney1[i] == strCountChinese[j]) {
dResult[i+nLargeLen] = nCount[j];
}
}
for (int j = 0 ; j<strUnitChinese.length ; ++j){
if (charMoney1[i] == strUnitChinese[j]){
dResult[i+nLargeLen] = dUnit[j];
}
}
} double Result = 0;
for (int i = 0 ; i<dResult.length ; ++i)
System.out.println(dResult[i]); for (int i = 0 ; i<dResult.length ; ){
if (dResult[i]==0.0){
++i;
continue;
}
Result =Result+ dResult[i]*dResult[i+1];
i = i+2;
}
System.out.println("Result:"+Result);