[code] public static String changeNumber(String str){ int i = str.indexOf('万'); String result = ""; if(i == -1){ result = belowTenThousand(str); }else{ result = belowTenThousand(str.substring(0, i)); result += belowTenThousand(str.substring(i + 1)); } return result; }
String result = ""; int flag = 0; for(int i = 0; i < str.length(); i++){ int num = numbers.indexOf(str.charAt(i));
if(num == 0){
}else if(num >= 1 && num <= 9){
}else if(num == 10){ flag = 1; }else if(num == 11){ flag = 2; }else if(num == 12){ flag = 3; } }
return result; } [/code] 按万分割...剩了的自己填
public class Test { public static void main(String[] args) { String str = "一千零五十万元"; System.out.println(changeNumber(str)); }
public static String changeNumber(String str){ int i = str.indexOf('万'); String result = ""; if(i == -1){ result = belowTenThousand(str); }else{ result = belowTenThousand(str.substring(0, i)); result += belowTenThousand(str.substring(i + 1)); } return result; }
String result = ""; int flag = 0; for(int i = 0; i < str.length(); i++){ int num = numbers.indexOf(str.charAt(i));
if(num == 0){
}else if(num >= 1 && num <= 9){
}else if(num == 10){ flag = 1; }else if(num == 11){ flag = 2; }else if(num == 12){ flag = 3; } }
return result; } }
package mars.test;import java.util.regex.*;/** * 支持范围0-99999999元 * @author hKF26330 */ public class ConvertMoney { public static final int Zero = 0; public static final int One = 1; public static final int Two = 2; public static final int Three = 3; public static final int Four = 4; public static final int Five = 5; public static final int Six = 6; public static final int Seven = 7; public static final int Eight = 8; public static final int Nine = 9; public static final int Ten = 10; public static final int Bai = 100; public static final int Qian = 1000; public static final int Wan = 10000; public static final String FullPattern = "^([零一二三四五六七八九十千百]+万)?[零]?([一二三四五六七八九]{1}千)?[零]?([一二三四五六七八九]{1}百)?[零]?([一二三四五六七八九]{1}十)?([一二三四五六七八九])?元$"; public static final String WanPattern = "^([一二三四五六七八九]{1}千)?[零]?([一二三四五六七八九]{1}百)?[零]?([一二三四五六七八九]{1}十)?([一二三四五六七八九十])?万$"; public static void main(String[] args) { System.out.println(getMoney("零元")); System.out.println(getMoney("一千五百元")); System.out.println(getMoney("一千零五十万一千零八十元")); System.out.println(getMoney("九千九百九十九万九千九百九十九元")); } /** * 判断给的字符串是否匹配 */ public static boolean isMatch(String money) { return Pattern.matches("^[零一二三四五六七八九十万千百]*元$", money); } public static int getConst(char c) { int returnValue = 0; switch (c) { case '零': returnValue=Zero; case '一': returnValue=One; break; case '二': returnValue=Two; break; case '三': returnValue=Three; break; case '四': returnValue=Four; break; case '五': returnValue=Five; break; case '六': returnValue=Six; break; case '七': returnValue=Seven; break; case '八': returnValue=Eight; break; case '九': returnValue=Nine; break; case '十': returnValue=Ten; break; case '万': returnValue=Wan; break; case '千': returnValue=Qian; break; case '百': returnValue=Bai; break; } return returnValue; }
/** * 获取每单位的值 * @param value * @return */ public static int getValue(String value) { int returnValue = 0; if (value.length()==1) { returnValue=getConst(value.charAt(0)); } if (value.length() == 2) { returnValue=getConst(value.charAt(0))*getConst(value.charAt(1)); } return returnValue; } public static int getMoney(String money) { int returnValue = 0;
public static String changeNumber(String str){
int i = str.indexOf('万');
String result = "";
if(i == -1){
result = belowTenThousand(str);
}else{
result = belowTenThousand(str.substring(0, i));
result += belowTenThousand(str.substring(i + 1));
}
return result;
}
private static String belowTenThousand(String str){
String numbers = "零一二三四五六七八九十百千元";
String result = "";
int flag = 0;
for(int i = 0; i < str.length(); i++){
int num = numbers.indexOf(str.charAt(i));
if(num == 0){
}else if(num >= 1 && num <= 9){
}else if(num == 10){
flag = 1;
}else if(num == 11){
flag = 2;
}else if(num == 12){
flag = 3;
}
}
return result;
}
[/code]
按万分割...剩了的自己填
public class Test {
public static void main(String[] args) {
String str = "一千零五十万元";
System.out.println(changeNumber(str));
}
public static String changeNumber(String str){
int i = str.indexOf('万');
String result = "";
if(i == -1){
result = belowTenThousand(str);
}else{
result = belowTenThousand(str.substring(0, i));
result += belowTenThousand(str.substring(i + 1));
}
return result;
}
private static String belowTenThousand(String str){
String numbers = "零一二三四五六七八九十百千元";
String result = "";
int flag = 0;
for(int i = 0; i < str.length(); i++){
int num = numbers.indexOf(str.charAt(i));
if(num == 0){
}else if(num >= 1 && num <= 9){
}else if(num == 10){
flag = 1;
}else if(num == 11){
flag = 2;
}else if(num == 12){
flag = 3;
}
}
return result;
}
}
* 支持范围0-99999999元
* @author hKF26330
*/
public class ConvertMoney {
public static final int Zero = 0;
public static final int One = 1;
public static final int Two = 2;
public static final int Three = 3;
public static final int Four = 4;
public static final int Five = 5;
public static final int Six = 6;
public static final int Seven = 7;
public static final int Eight = 8;
public static final int Nine = 9;
public static final int Ten = 10;
public static final int Bai = 100;
public static final int Qian = 1000;
public static final int Wan = 10000; public static final String FullPattern = "^([零一二三四五六七八九十千百]+万)?[零]?([一二三四五六七八九]{1}千)?[零]?([一二三四五六七八九]{1}百)?[零]?([一二三四五六七八九]{1}十)?([一二三四五六七八九])?元$";
public static final String WanPattern = "^([一二三四五六七八九]{1}千)?[零]?([一二三四五六七八九]{1}百)?[零]?([一二三四五六七八九]{1}十)?([一二三四五六七八九十])?万$"; public static void main(String[] args) {
System.out.println(getMoney("零元"));
System.out.println(getMoney("一千五百元"));
System.out.println(getMoney("一千零五十万一千零八十元"));
System.out.println(getMoney("九千九百九十九万九千九百九十九元"));
} /**
* 判断给的字符串是否匹配
*/
public static boolean isMatch(String money) {
return Pattern.matches("^[零一二三四五六七八九十万千百]*元$", money);
} public static int getConst(char c) {
int returnValue = 0;
switch (c) {
case '零':
returnValue=Zero;
case '一':
returnValue=One;
break;
case '二':
returnValue=Two;
break;
case '三':
returnValue=Three;
break;
case '四':
returnValue=Four;
break;
case '五':
returnValue=Five;
break;
case '六':
returnValue=Six;
break;
case '七':
returnValue=Seven;
break;
case '八':
returnValue=Eight;
break;
case '九':
returnValue=Nine;
break;
case '十':
returnValue=Ten;
break;
case '万':
returnValue=Wan;
break;
case '千':
returnValue=Qian;
break;
case '百':
returnValue=Bai;
break;
}
return returnValue;
}
* 获取每单位的值
* @param value
* @return
*/
public static int getValue(String value) {
int returnValue = 0;
if (value.length()==1) {
returnValue=getConst(value.charAt(0));
}
if (value.length() == 2) {
returnValue=getConst(value.charAt(0))*getConst(value.charAt(1));
}
return returnValue;
} public static int getMoney(String money) {
int returnValue = 0;
if (isMatch(money)) {
Matcher matcher = Pattern.compile(FullPattern).matcher(money);
// 获取每个位(万、千、百、十、个)的文本
String wan = null;
String qian = null;
String bai = null;
String shi = null;
String yuan = null; while (matcher.find()) {
wan = matcher.group(1);
qian = matcher.group(2);
bai = matcher.group(3);
shi = matcher.group(4);
yuan = matcher.group(5);
} // 由于万位比较特殊,继续分析万位
String wan_qian = null;
String wan_bai = null;
String wan_shi = null;
String wan_ge = null; if (wan != null) {
Matcher matcherWan = Pattern.compile(WanPattern).matcher(wan);
while (matcherWan.find()) {
wan_qian = matcherWan.group(1);
wan_bai = matcherWan.group(2);
wan_shi = matcherWan.group(3);
wan_ge = matcherWan.group(4);
}
}
// 计算万位的真实值
if (wan != null) {
if (wan_qian != null) {
returnValue+=getValue(wan_qian);
}
if (wan_bai != null) {
returnValue+=getValue(wan_bai);
}
if (wan_shi != null) {
returnValue+=getValue(wan_shi);
}
if (wan_ge != null) {
returnValue+=getValue(wan_ge);
}
returnValue*=Wan;
}
// 计算千位的真实值
if (qian != null) {
returnValue+=getValue(qian);
}
// 计算百位的真实值
if (bai != null) {
returnValue+=getValue(bai);
}
// 计算十位的真实值
if (shi != null) {
returnValue+=getValue(shi);
}
// 计算个位的真实值
if (yuan != null) {
returnValue+=getValue(yuan);
}
}
return returnValue;
}}两部分组合一起