把字符串的中文数字转化为阿拉伯数字

	private static Pattern numPattern = Pattern.compile("\\d+");

         /**
	 * 把“火影第二部第三百二十回”转化为“火影第2部第320回”
	 * 
	 * @param s
	 * @return 对应字符串的阿拉伯数字形式
	 * @author chow 2010-8-19 上午11:04:35
	 */
	private static String cnNumToInt(String s) {
		StringBuffer result = new StringBuffer();
		int sum = 0;
		boolean cnInterupt = false; // 转化过程被中文间断

		int yi = 1;// 记录高级单位
		int wan = 1;// 记录高级单位
		int ge = 1;// 记录单位

		for (int i = s.length() - 1; i >= 0; i--) {
			char c = s.charAt(i);
			int temp = 0;// 记录数值
			switch (c) {
			// 数值
			case '〇':
			case '零':
				temp = 0;
				break;
			case '一':
				temp = 1 * ge * wan * yi;
				ge = 1;
				break;
			case '二':
				temp = 2 * ge * wan * yi;
				ge = 1;
				break;
			case '三':
				temp = 3 * ge * wan * yi;
				ge = 1;
				break;
			case '四':
				temp = 4 * ge * wan * yi;
				ge = 1;
				break;
			case '五':
				temp = 5 * ge * wan * yi;
				ge = 1;
				break;
			case '六':
				temp = 6 * ge * wan * yi;
				ge = 1;
				break;
			case '七':
				temp = 7 * ge * wan * yi;
				ge = 1;
				break;
			case '八':
				temp = 8 * ge * wan * yi;
				ge = 1;
				break;
			case '九':
				temp = 9 * ge * wan * yi;
				ge = 1;
				break;
			// 单位,前缀是单数字
			case '十':
				ge = 10;
				break;
			case '百':
				ge = 100;
				break;
			case '千':
				ge = 1000;
				break;
			// 高级单位,前缀可以是多个数字
			case '万':
				wan = 10000;
				ge = 1;
				break;
			case '亿':
				yi = 100000000;
				wan = 1;
				ge = 1;
				break;
			default:
				cnInterupt = true;
			}
			// 先把当前累加的sum拼到原字符串,再把非中文数字 字符拼回原字符串
			if (cnInterupt) {
				if (sum != 0) {
					result.insert(0, sum);
				}
				result.insert(0, c);
				cnInterupt = false;
				sum = temp = 0;
				ge = wan = yi = 1;
			}
			if (ge == 10 && (i==0||numPattern.matcher(s.substring(i-1,i)).find())) { // 对第十集,第十二,十五的特殊处理
				temp = 10;
			}
			sum += temp;
		}
		if (sum != 0) {
			result.insert(0, sum);
		}
		return result.toString();
	}

 

PS:程序是改写网上的,网上的程序是只能转化全部都为中文数字的字符串,本程序可处理含有其他中文的字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值