[java]代码库
package com.ly.changenum;
/**
* 把中文数字转换为阿拉伯数字,把阿拉伯数字转换为中文数字
*
* @author zhaigy
* @date 2010-2-20
*/
final public class Changenum {
public static void main(String[] args) {
String a =cnNumToInt("二零〇一年五月一日");
System.err.println(a);
}
static public String cnNumToInt(String s) {
String year = s.substring(0,s.indexOf("年"));
String month = s.substring(s.indexOf("年")+1,s.indexOf("月"));
String day = s.substring(s.indexOf("月")+1,s.indexOf("日"));
String year_int =changgedate(year);
String month_int =changgedate(month);
String day_int = changgedate(day);
return ""+year_int+month_int+day_int;
}
public static String changgedate(String strs){
String result="";
boolean flag =false;
if(strs.startsWith("十")){
flag=true;
}
int size =strs.length();
for(int i =1, j=0;i<=strs.length();i++,j++){
String str = strs.substring(j,i);
switch (str) {
case "〇":
result+=0;
break;
case "零":
result+=0;
break;
case "一":
result+=1;
break;
case "二":
result+=2;
break;
case "三":
result+=3;
break;
case "四":
result+=4;
break;
case "五":
result+=5;
break;
case "六":
result+=6;
break;
case "七":
result+=7;
break;
case "八":
result+=8;
break;
case "九":
result+=9;
break;
case "十":
if(flag&&size==1){
result+=10;
break;
}
if(!flag&&size==2){
result+=0;
break;
}
if(size==3){
break;
}
else{
result+=1;
break;
}
}
}
if(!flag&&size==1){
return "0"+Integer.parseInt(result);
}
else
return Integer.parseInt(result)+"";
}
}
高级设计师
by: 神马 发表于:2012-09-27 11:37:01 顶(0) | 踩(0) 回复
回复评论