在日常生活中,我們經(jīng)常會將阿拉伯?dāng)?shù)字轉(zhuǎn)化為中文大寫的情況:"零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖", "拾", "佰", "仟", "萬", "億",因此自己就編寫一個類,實現(xiàn)該功能的轉(zhuǎn)化。
實現(xiàn)過程:
對于這個問題的實現(xiàn),我們首先把數(shù)字按四位一個拆開(符合我們讀數(shù)字的習(xí)慣),然后對這四位數(shù),做出他的讀法,具體代碼如下:
private static String getNumberStr1000 (int num) {
if (num > 9999 || num 0) {
return "";
}
int q = num / 1000;
int b = (num / 100) % 10;
int s = (num / 10) % 10;
int g = num % 10;
StringBuffer sb = new StringBuffer();
//千
if (q > 0) {
sb.append(NUMBER1[q]);
sb.append(NUMBER2[3]);
}
//百
if (b > 0) {
sb.append(NUMBER1[b]);
sb.append(NUMBER2[2]);
} else {
if (q != 0) {
sb.append(NUMBER2[0]);
}
}
//十
if (s > 0) {
sb.append(NUMBER1[s]);
sb.append(NUMBER2[1]);
} else {
if (b != 0) {
sb.append(NUMBER2[0]);
}
}
//個
if (g > 0) {
sb.append(NUMBER1[g]);
}
return sb.toString();
}
下面我們只需要在編寫一個方法,實現(xiàn)每個四位數(shù)的讀法以及對應(yīng)的單位即可,具體代碼如下:
public static String getNumberStr(int num) {
if (num 0) {
return "";
}
if (num == 0) {
return NUMBER1[0];
}
int split = 10000;
int y = num / (split * split);
int w = (num / split) % split;
int g = num % split;
StringBuffer sb = new StringBuffer();
//億
if (y > 0) {
sb.append(getNumberStr1000(y));
sb.append(NUMBER2[5]);
}
//萬
if (w > 999) {
sb.append(getNumberStr1000(w));
sb.append(NUMBER2[4]);
} else {
if (w > 0) {
if (y != 0) {
sb.append(NUMBER2[0]);
}
sb.append(getNumberStr1000(w));
sb.append(NUMBER2[4]);
}
}
//萬以下
if (g > 0) {
if (w != 0) {
if (g > 999) {
sb.append(getNumberStr1000(g));
} else {
sb.append(NUMBER2[0]);
sb.append(getNumberStr1000(g));
}
} else {
if (y != 0) {
sb.append(NUMBER2[0]);
}
sb.append(getNumberStr1000(g));
}
}
return sb.toString();
}
完整源代碼:
/**
*@Description:
*/
package com.lulei.util;
public class NumberUtil {
private static String[] NUMBER1 = {"零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖"};
private static String[] NUMBER2 = {"零", "拾", "佰", "仟", "萬", "億"};
/**
* @param num
* @return
* @Author:lulei
* @Description:將數(shù)字轉(zhuǎn)化為大寫
*/
public static String getNumberStr(int num) {
if (num 0) {
return "";
}
if (num == 0) {
return NUMBER1[0];
}
int split = 10000;
int y = num / (split * split);
int w = (num / split) % split;
int g = num % split;
StringBuffer sb = new StringBuffer();
//億
if (y > 0) {
sb.append(getNumberStr1000(y));
sb.append(NUMBER2[5]);
}
//萬
if (w > 999) {
sb.append(getNumberStr1000(w));
sb.append(NUMBER2[4]);
} else {
if (w > 0) {
if (y != 0) {
sb.append(NUMBER2[0]);
}
sb.append(getNumberStr1000(w));
sb.append(NUMBER2[4]);
}
}
//萬以下
if (g > 0) {
if (w != 0) {
if (g > 999) {
sb.append(getNumberStr1000(g));
} else {
sb.append(NUMBER2[0]);
sb.append(getNumberStr1000(g));
}
} else {
if (y != 0) {
sb.append(NUMBER2[0]);
}
sb.append(getNumberStr1000(g));
}
}
return sb.toString();
}
/**
* @param num
* @return
* @Description:對萬以下的數(shù)字進(jìn)行大小寫轉(zhuǎn)化
*/
private static String getNumberStr1000 (int num) {
if (num > 9999 || num 0) {
return "";
}
int q = num / 1000;
int b = (num / 100) % 10;
int s = (num / 10) % 10;
int g = num % 10;
StringBuffer sb = new StringBuffer();
//千
if (q > 0) {
sb.append(NUMBER1[q]);
sb.append(NUMBER2[3]);
}
//百
if (b > 0) {
sb.append(NUMBER1[b]);
sb.append(NUMBER2[2]);
} else {
if (q != 0) {
sb.append(NUMBER2[0]);
}
}
//十
if (s > 0) {
sb.append(NUMBER1[s]);
sb.append(NUMBER2[1]);
} else {
if (b != 0) {
sb.append(NUMBER2[0]);
}
}
//個
if (g > 0) {
sb.append(NUMBER1[g]);
}
return sb.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
System.out.println(i + "--" + NumberUtil.getNumberStr(i));
i = 1001;
System.out.println(i + "--" + NumberUtil.getNumberStr(i));
i = 100101;
System.out.println(i + "--" + NumberUtil.getNumberStr(i));
i = 10100101;
System.out.println(i + "--" + NumberUtil.getNumberStr(i));
i = 1234567890;
System.out.println(i + "--" + NumberUtil.getNumberStr(i));
}
}
測試運行結(jié)果:
希望本文所述對大家學(xué)習(xí)java程序設(shè)計有所幫助。
您可能感興趣的文章:- java中文轉(zhuǎn)拼音工具類詳解
- java中unicode和中文相互轉(zhuǎn)換的簡單實現(xiàn)
- java中文轉(zhuǎn)全拼工具類分享
- java unicode轉(zhuǎn)碼為中文實例
- java數(shù)字轉(zhuǎn)漢字工具類詳解
- java金額數(shù)字轉(zhuǎn)中文工具類詳解