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, "二十億零五百零一萬零一十")
// new Test_Data(4009060200, "四十億零九百零六萬零二百"),
// new Test_Data(4294967295, "四十二億九千四百九十六萬七千二百九十五")
};
/**
* 阿拉伯數字轉換爲中文數字的核心算法實現。
* @param num 爲需要轉換爲中文數字的阿拉伯數字,是無符號的整形數
* @return
*/
public static String NumberToChn(int num) {
StringBuffer returnStr = new StringBuffer();
Boolean needZero = false;
int pos=0; //節權位的位置
if(num==0){
//如果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); //判斷section中的千位上是不是爲零,若爲零應該添加一個零。
pos++;
num = num / 10000;
}
return returnStr.toString();
}
/**
* 將四位的section轉換爲中文數字
* @param section
* @return
*/
public static String SectionNumToChn(int section) {
StringBuffer returnStr = new StringBuffer();
int unitPos = 0; //節權位的位置編號,0-3依次爲個十百千;
Boolean zero = true;
while (section > 0) {
int v = (section % 10);
if (v == 0) {
if ((section == 0) || !zero) {
zero = true; /*需要補0,zero的作用是確保對連續的多個0,只補一箇中文零*/
//chnStr.insert(0, chnNumChar[v]);
returnStr.insert(0, CHN_NUMBER[v]);
}
} else {
zero = false; //至少有一個數字不是0
StringBuffer tempStr = new StringBuffer(CHN_NUMBER[v]);//數字v所對應的中文數字
tempStr.append(CHN_UNIT[unitPos]); //數字v所對應的中文權位
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));
}
}
/**
* 中文轉換成阿拉伯數字,中文字符串除了包括0-9的中文漢字,還包括十,百,千,萬等權位。
* 此處是完成對這些權位的類型定義。
* name是指這些權位的漢字字符串。
* value是指權位多對應的數值的大小。諸如:十對應的值的大小爲10,百對應爲100等
* secUnit若爲true,代表該權位爲節權位,即萬,億,萬億等
*/
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)
};
/**
* 返回中文數字漢字所對應的阿拉伯數字,若str不爲中文數字,則返回-1
* @param str
* @return
*/
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;
}
/**
* 返回中文漢字權位在chnNameValue數組中所對應的索引號,若不爲中文漢字權位,則返回-1
* @param str
* @return
*/
public static int ChnUnitToValue(String str){
for(int i=0;i<chnNameValue.length;i++){
if(str.equals(chnNameValue[i].name)){
return i;
}
}
return -1;
}
/**
* 返回中文數字字符串所對應的int類型的阿拉伯數字
* @param str
* @return
*/
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));
//若num>=0,代表該位置(pos),所對應的是數字不是權位。若小於0,則表示爲權位
if(num>=0){
number=num;
pos++;
//pos是最好一位,直接將number加入到section中。
if(pos>=str.length()){
section+=number;
returnNumber+=section;
break;
}
}else{
int chnNameValueIndex=ChnUnitToValue(str.substring(pos,pos+1));
//chnNameValue[chnNameValueIndex].secUnit==true,表示該位置所對應的權位是節權位,
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) {
// TestNumToChn();
// System.out.println("--------------------------------------------------------");
// TestChnStringToNumber();
String s = NumberToChn(15);
String s1 = SectionNumToChn(13);
String s3 = NumberToChn(29);
System.out.println(s+"=="+s1+"=="+s3);
}
}
如何將數字轉化爲中文大寫 java
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.