汉字用零一二三四五六七八九作为基本计数,与阿拉伯数字靠数字偏移位置不一样。
中文数字是 “数字+权位”的方式组成数字,比如百,千,万。
中文数字每个数字后都会跟上一个权位,权位就是数字的量值,相当于阿拉伯数字的数位
中文计数以万为小节,万以下没有节权,万以上是亿为节权。
中文还有一个特点是多变的零,大概总结为三个规则:
- 以10000为小节,小节的结尾即使是0,也不使用“零”
- 小节内两个非0数字之间要使用“零”
- 当小节的千位是0时,若小节的前一小节无其他数字,则不用零,否则就要用“零”
首先,阿拉伯数字转中文数字
public class toChineseNumUtill {
public static String numberToChinese(int num){
if(num == 0){
return "零";
}
int weigth = 0;//节权位
String chinese="";
String chinese_section = "";
boolean setZero =false;//下一小节是否需要零,第一次没有上一小节所以为false
while (num >0){
int section = num%10000;//得到最后面的小节
if(setZero){//判断上一小节的千位是否为零,是就设置零
chinese = TooltoCh(0)+chinese;
}
chinese_section = sectionTrans(section);
if(section != 0){//判断是都加节权位
chinese_section = chinese_section +getWeight(weigth);
}
chinese = chinese_section+chinese;
chinese_section = "";
setZero = (section<1000)&&(section>0);
num = num/10000;
weigth++;
}
if((chinese.length()==2||(chinese.length() == 3))&&chinese.contains("一十")){
chinese = chinese.substring(1,chinese.length());
}
return chinese;
}
public static String sectionTrans(int section){
StringBuilder section_chinese =
new StringBuilder();
int pos = 0;//小节内部权位的计数器
boolean zero = true;//小节内部的置零判断,每一个小节只能有一个零。
while (section >0){
int v = section%10;//得到最后一个数
if(v ==0 ){
if(!zero){
zero = true;//需要补零的操作,确保对连续多个零只是输出一个
section_chinese.insert(0,TooltoCh(0));
}
}else {
zero = false;//有非零数字就把置零打开
section_chinese.insert(0,getPos(pos));
section_chinese.insert(0,TooltoCh(v));
}
pos++;
section = section/10;
}
return section_chinese.toString();
}
public static String TooltoCh(int num){
switch (num){
case 0:
return "零";
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
case 6:
return "六";
case 7:
return "七";
case 8:
return "八";
case 9:
return "九";
}
return "erro";
}
public static String getPos(int pos){
switch (pos){
case 1:
return "十";
case 2:
return "百";
case 3:
return "千";
}
return "";
}
public static String getWeight(int weight){
switch (weight){
case 1:
return "万";
case 2:
return "亿";
}
return "";
}
}
这段代码我只到了亿
中文转阿拉伯数字
public class ChineseChangeToNumber {
public int ChineseToNumber(String str){
String str1 = new String();
String str2 = new String();
String str3 = new String();
int k = 0;
boolean dealflag = true;
for(int i=0;i<str.length();i++){//先把字符串中的“零”除去
if('零' == (str.charAt(i))){
str = str.substring(0, i) + str.substring(i+1);
}
}
String chineseNum = str;
for(int i=0;i<chineseNum.length();i++){
if(chineseNum.charAt(i) == '亿'){
str1 = chineseNum.substring(0,i);//截取亿前面的数字,逐个对照表格,然后转换
k = i+1;
dealflag = false;//已经处理
}
if(chineseNum.charAt(i) == '万'){
str2 = chineseNum.substring(k,i);
str3 = str.substring(i+1);
dealflag = false;//已经处理
}
}
if(dealflag){//如果没有处理
str3 = chineseNum;
}
int result = sectionChinese(str1) * 100000000 +
sectionChinese(str2) * 10000 + sectionChinese(str3);
return result;
}
public int sectionChinese(String str){
int value = 0;
int sectionNum = 0;
for(int i=0;i<str.length();i++){
int v = (int) Tool.intList.get(str.charAt(i));
if( v == 10 || v == 100 || v == 1000 ){//如果数值是权位则相乘
sectionNum = v * sectionNum;
value = value + sectionNum;
}else if(i == str.length()-1){
value = value + v;
}else{
sectionNum = v;
}
}
return value;
}
}
工具类:
public class Tool {
//数字位
public static String[] chnNumChar = {"零","一","二","三","四","五","六","七","八","九"};
public static char[] chnNumChinese = {'零','一','二','三','四','五','六','七','八','九'};
//节权位
public static String[] chnUnitSection = {"","万","亿","万亿"};
//权位
public static String[] chnUnitChar = {"","十","百","千"};
public static HashMap intList = new HashMap();
static{
for(int i=0;i<chnNumChar.length;i++){
intList.put(chnNumChinese[i], i);
}
intList.put('十',10);
intList.put('百',100);
intList.put('千', 1000);
}
}
本篇来源:
http://blog.csdn.net/jjfly999/article/details/51052492