package com.tchf.service.plan.common.utils;
public class NumberToChn {
static String CHN_NUMBER[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
static String CHN_UNIT[] = {"", "十", "百", "千"};
static String CHN_UNIT_SECTION[] = {"", "万", "亿", "万亿"};
public static class Test_Data{
int number;
String chnNum;
public Test_Data(int number,String chnNum){
this.chnNum=chnNum;
this.number=number;
}
}
static Test_Data testData[]={
new Test_Data(0,"零"),
new Test_Data(1,"一"),
new Test_Data(2,"二"),
new Test_Data(3, "三"),
new Test_Data(4, "四"),
new Test_Data(5, "五"),
new Test_Data(6, "六"),
new Test_Data(7, "七"),
new Test_Data(8, "八"),
new Test_Data(9, "九"),
new Test_Data(10, "十"),
new Test_Data(11, "十一"),
new Test_Data(110, "一百一十"),
new Test_Data(111, "一百一十一"),
new Test_Data(100, "一百"),
new Test_Data(102, "一百零二"),
new Test_Data(1020, "一千零二十"),
new Test_Data(1001, "一千零一"),
new Test_Data(1015, "一千零一十五"),
new Test_Data(1000, "一千"),
new Test_Data(10000, "一万"),
new Test_Data(20010, "二万零一十"),
new Test_Data(20001, "二万零一"),
new Test_Data(100000, "一十万"),
new Test_Data(1000000, "一百万"),
new Test_Data(10000000, "一千万"),
new Test_Data(100000000, "一亿"),
new Test_Data(1000000000, "一十亿"),
new Test_Data(1000001000, "一十亿一千"),
new Test_Data(1000000100, "一十亿零一百"),
new Test_Data(200010, "二十万零一十"),
new Test_Data(2000105, "二百万零一百零五"),
new Test_Data(20001007, "二千万一千零七"),
new Test_Data(2000100190, "二十亿零一十万零一百九十"),
new Test_Data(1040010000, "一十亿四千零一万"),
new Test_Data(200012301, "二亿零一万二千三百零一"),
new Test_Data(2005010010, "二十亿零五百零一万零一十")
};
public static String NumberToChn(int num) {
StringBuffer returnStr = new StringBuffer();
Boolean needZero = false;
int pos=0;
if(num==0){
returnStr.insert(0,CHN_NUMBER[0]);
}
if(num >9 && num< 20){
int section = num % 10;
if(section==0){
return new StringBuffer("十").toString();
}
return new StringBuffer("十").append(CHN_NUMBER[section]).toString();
}
while (num > 0) {
int section = num % 10000;
if (needZero) {
returnStr.insert(0, CHN_NUMBER[0]);
}
String sectionToChn = SectionNumToChn(section);
sectionToChn += (section != 0) ? CHN_UNIT_SECTION[pos] : CHN_UNIT_SECTION[0];
returnStr.insert(0, sectionToChn);
needZero = ((section < 1000 && section > 0) ? true : false);
pos++;
num = num / 10000;
}
return returnStr.toString();
}
public static String SectionNumToChn(int section) {
StringBuffer returnStr = new StringBuffer();
int unitPos = 0;
Boolean zero = true;
while (section > 0) {
int v = (section % 10);
if (v == 0) {
if ((section == 0) || !zero) {
zero = true;
returnStr.insert(0, CHN_NUMBER[v]);
}
} else {
zero = false;
StringBuffer tempStr = new StringBuffer(CHN_NUMBER[v]);
tempStr.append(CHN_UNIT[unitPos]);
returnStr.insert(0, tempStr);
}
unitPos++;
section = section / 10;
}
return returnStr.toString();
}
public static void TestNumToChn(){
for(int i=0;i<testData.length;i++) {
String str=NumberToChn(testData[i].number);
System.out.println(testData[i].number+"\t"+testData[i].chnNum+"\t"+str+"\t"+str.equals(testData[i].chnNum));
}
}
public static class Chn_Name_value{
String name;
int value;
Boolean secUnit;
public Chn_Name_value(String name,int value,Boolean secUnit){
this.name=name;
this.value=value;
this.secUnit=secUnit;
}
}
static Chn_Name_value chnNameValue[]={
new Chn_Name_value("十",10,false),
new Chn_Name_value("百",100,false),
new Chn_Name_value("千",1000,false),
new Chn_Name_value("万",10000,true),
new Chn_Name_value("亿",100000000,true)
};
public static int ChnNumToValue(String str){
for(int i=0;i<CHN_NUMBER.length;i++){
if(str.equals(CHN_NUMBER[i])){
return i;
}
}
return -1;
}
public static int ChnUnitToValue(String str){
for(int i=0;i<chnNameValue.length;i++){
if(str.equals(chnNameValue[i].name)){
return i;
}
}
return -1;
}
public static int ChnStringToNumber(String str){
int returnNumber=0;
int section=0;
int pos=0;
int number=0;
while (pos<str.length()){
int num=ChnNumToValue(str.substring(pos,pos+1));
if(num>=0){
number=num;
pos++;
if(pos>=str.length()){
section+=number;
returnNumber+=section;
break;
}
}else{
int chnNameValueIndex=ChnUnitToValue(str.substring(pos,pos+1));
if(chnNameValue[chnNameValueIndex].secUnit){
section=(section+number)*chnNameValue[chnNameValueIndex].value;
returnNumber+=section;
section=0;
}else{
section+=number*chnNameValue[chnNameValueIndex].value;
}
pos++;
number=0;
if(pos>=str.length()){
returnNumber+=section;
break;
}
}
}
return returnNumber;
}
public static void TestChnStringToNumber(){
for(int i=0;i<testData.length;i++){
int number=ChnStringToNumber(testData[i].chnNum);
System.out.println(testData[i].chnNum+"\t"+number+"\t"+testData[i].number+"\t"+(number==testData[i].number));
}
}
public static void main(String[] args) {
String s = NumberToChn(15);
String s1 = SectionNumToChn(13);
String s3 = NumberToChn(29);
System.out.println(s+"=="+s1+"=="+s3);
}
}