java实现中文数字与阿拉伯数字互相转换
最近在作東西的時(shí)候,需要使用中文數(shù)字與阿拉伯?dāng)?shù)字進(jìn)行互相轉(zhuǎn)換.在網(wǎng)搜了一下效果并不是十分理想.沒有辦法只好自己動手.
??? 現(xiàn)在拿出來與大家分享,并不斷完善.
?
需要注意的一些地方:
1.??? 目前最大上限是以億為單位.
2.??? 沒有實(shí)現(xiàn)中文大寫錢數(shù)的互換.
3.??? 實(shí)用的是int型數(shù)字,這樣上限大概是以十億為單位.
4.??? 有些中文的數(shù)字習(xí)慣沒有處理(“4億”=“4萬萬”,“一十一”=“十一”,“零”=“0”等等)
?
Java代碼如下
?
/**
?*
?*
?*
?*@author<a href="yzy0612@163.com">yangzhenyu</a>
?*
?*@version$Revision$
?*
?*@since2010-8-26
?*/
publicclass NumberUtils {
??????
??? /*
??? ?* 基本數(shù)字單位;
??? ?*/
??? privatestaticfinal String[] units =
?????? { "千", "百", "十", "" };// 個(gè)位
???
??? /*
??? ?* 大數(shù)字單位;
??? ?*/
??? privatestaticfinal String[] bigUnits =
?????? { "萬", "億" };
???
??? /*
??? ?* 中文數(shù)字;
??? ?*/
??? privatestaticfinalchar[] numChars =
?????? { '一', '二', '三', '四', '五', '六', '七', '八', '九' };
???
//? private static final char[] numMouneyChars =
//???? { '壹', '貳', '叁', '肆', '伍', '陸', '柒', '捌', '玖' };
???
??? privatestaticcharnumZero = '零';
???
??? /**
??? ?*將中文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字;
??? ?*
??? ?*@paramnumberCN
??? ?*@return
??? ?*/
??? publicstaticint numberCN2Arab(String numberCN) {
??????
?????? String tempNumberCN = numberCN;
??????
?????? // 異常數(shù)據(jù)處理;
?????? if (tempNumberCN == null) {
?????????? return 0;
?????? }
??????
?????? /*
?????? ?* nums[0] 保存以千單位; nums[1] 保存以萬單位; nums[2] 保存以億單位;
?????? ?*/
?????? String[] nums = new String[bigUnits.length + 1];
??????
?????? //千位以內(nèi),直接處理;
?????? nums[0] = tempNumberCN;
??????
?????? /*
?????? ?* 分割大數(shù)字,以千為單位進(jìn)行運(yùn)算;
?????? ?*/
?????? for (int i = (bigUnits.length - 1); i >= 0; i--) {
??????????
?????????? // 是否存在大單位(萬,億...);
?????????? int find = tempNumberCN.indexOf(bigUnits[i]);
??????????
?????????? if (find != -1) {
????????????? String[] tempStrs = tempNumberCN.split(bigUnits[i]);
?????????????
????????????? //清空千位內(nèi)容;
????????????? if (nums[0] != null) {
????????????????? nums[0] = null;
????????????? }
?????????????
????????????? if (tempStrs[0] != null) {
????????????????? nums[i + 1] = tempStrs[0];
????????????? }
?????????????
????????????? if (tempStrs.length > 1) {
????????????????? tempNumberCN = tempStrs[1];
?????????????????
????????????????? if (i == 0) {
???????????????????? nums[0] = tempStrs[1];
????????????????? }
?????????????????
????????????? } else {
????????????????? tempNumberCN = null;
?????????????????
????????????????? break;
????????????? }
?????????? }
?????? }
??????
?????? String tempResultNum = "";
??????
?????? for (int i = nums.length - 1; i >= 0; i--) {
?????????? if (nums[i] != null) {
????????????? tempResultNum += numberKCN2Arab(nums[i]);
?????????? } else {
????????????? tempResultNum += "0000";
?????????? }
?????? }
??????
?????? return Integer.parseInt(tempResultNum);
??? }
???
??? /**
??? ?*將一位中文數(shù)字轉(zhuǎn)換為一位數(shù)字;eg:一返回1;
??? ?*
??? ?*@paramonlyCNNumber
??? ?*@return
??? ?*/
??? publicstaticint numberCharCN2Arab(char onlyCNNumber) {
??????
?????? if (numChars[0] == onlyCNNumber) {
?????????? return 1;
?????? } elseif (numChars[1] == onlyCNNumber || onlyCNNumber == '兩') {// 處理中文習(xí)慣用法(二,兩)
?????????? return 2;
?????? } elseif (numChars[2] == onlyCNNumber) {
?????????? return 3;
?????? } elseif (numChars[3] == onlyCNNumber) {
?????????? return 4;
?????? } elseif (numChars[4] == onlyCNNumber) {
?????????? return 5;
?????? } elseif (numChars[5] == onlyCNNumber) {
?????????? return 6;
?????? } elseif (numChars[6] == onlyCNNumber) {
?????????? return 7;
?????? } elseif (numChars[7] == onlyCNNumber) {
?????????? return 8;
?????? } elseif (numChars[8] == onlyCNNumber) {
?????????? return 9;
?????? }
??????
?????? return 0;
??? }
???
??? /**
??? ?*將一位數(shù)字轉(zhuǎn)換為一位中文數(shù)字;eg:1返回一;
??? ?*
??? ?*@paramonlyArabNumber
??? ?*@return
??? ?*/
??? publicstaticchar numberCharArab2CN(char onlyArabNumber) {
??????
?????? if (onlyArabNumber == '0') {
?????????? returnnumZero;
?????? }
??????
?????? if (onlyArabNumber > '0' && onlyArabNumber <= '9') {
?????????? returnnumChars[onlyArabNumber - '0' - 1];
?????? }
??????
?????? return onlyArabNumber;
??? }
???
??? /**
??? ?*
??? ?*@paramnum
??? ?*@return
??? ?*/
??? publicstatic String numberArab2CN(Integer num) {
??????
?????? String tempNum = num + "";
??????
?????? // 傳說中的分頁算法;
?????? int numLen = tempNum.length();
?????? int start = 0;
?????? int end = 0;
?????? int per = 4;
?????? int total = (int) ((numLen + per - 1) / per);
?????? int inc = numLen % per;
??????
?????? /*
?????? ?* 123,1234,1234 四位一段,進(jìn)行處理;
?????? ?*/
?????? String[] numStrs = new String[total];
??????
?????? for (int i = total - 1; i >= 0; i--) {
?????????? start = (i - 1) * per + inc;
??????????
?????????? if (start < 0) {
????????????? start = 0;
?????????? }
??????????
?????????? end = i * per + inc;
??????????
?????????? numStrs[i] = tempNum.substring(start, end);
?????? }
??????
?????? String tempResultNum = "";
??????
?????? int rempNumsLen = numStrs.length;
?????? for (int i = 0; i < rempNumsLen; i++) {
??????????
?????????? // 小于1000補(bǔ)零處理;
?????????? if (i > 0 && Integer.parseInt(numStrs[i]) < 1000) {
????????????? tempResultNum += numZero
???????????????????? + numberKArab2CN(Integer.parseInt(numStrs[i]));
?????????? } else {
????????????? tempResultNum += numberKArab2CN(Integer.parseInt(numStrs[i]));
?????????? }
??????????
?????????? // 加上單位(萬,億....)
?????????? if (i < rempNumsLen - 1) {
????????????? tempResultNum += bigUnits[rempNumsLen - i - 2];
?????????? }
??????????
?????? }
??????
?????? // 去掉未位的零
?????? tempResultNum = tempResultNum.replaceAll(numZero + "$", "");
??????
?????? return tempResultNum;
??? }
???
??? /**
??? ?*將千以內(nèi)的數(shù)字轉(zhuǎn)換為中文數(shù)字;
??? ?*
??? ?*@paramnum
??? ?*@return
??? ?*/
??? privatestatic String numberKArab2CN(Integer num) {
??????
?????? char[] numChars = (num + "").toCharArray();
??????
?????? String tempStr = "";
??????
?????? int inc = units.length - numChars.length;
??????
?????? for (int i = 0; i < numChars.length; i++) {
?????????? if (numChars[i] != '0') {
????????????? tempStr += numberCharArab2CN(numChars[i]) + units[i + inc];
?????????? } else {
????????????? tempStr += numberCharArab2CN(numChars[i]);
?????????? }
?????? }
??????
?????? // 將連續(xù)的零保留一個(gè)
?????? tempStr = tempStr.replaceAll(numZero + "+", numZero + "");
??????
?????? // 去掉未位的零
?????? tempStr = tempStr.replaceAll(numZero + "$", "");
??????
?????? return tempStr;
??????
??? }
???
??? /**
??? ?*處理千以內(nèi)中文數(shù)字,返回4位數(shù)字字符串,位數(shù)不夠以"0"補(bǔ)齊;
??? ?*
??? ?*@paramnumberCN
??? ?*@return
??? ?*/
??? privatestatic String numberKCN2Arab(String numberCN) {
??????
?????? if ("".equals(numberCN)) {
?????????? return"";
?????? }
??????
?????? int[] nums = newint[4];
??????
?????? if (numberCN != null) {
??????????
?????????? for (int i = 0; i < units.length; i++) {
????????????? int idx = numberCN.indexOf(units[i]);
?????????????
????????????? if (idx > 0) {
????????????????? char tempNumChar = numberCN.charAt(idx - 1);
?????????????????
????????????????? int tempNumInt = numberCharCN2Arab(tempNumChar);
?????????????????
????????????????? nums[i] = tempNumInt;
????????????? }
?????????? }
??????????
?????????? // 處理十位
?????????? char ones = numberCN.charAt(numberCN.length() - 1);
?????????? nums[nums.length - 1] = numberCharCN2Arab(ones);
??????????
?????????? // 處理個(gè)位
?????????? if ((numberCN.length() == 2 || numberCN.length() == 1)
????????????????? && numberCN.charAt(0) == '十') {
????????????? nums[nums.length - 2] = 1;
?????????? }
?????? }
??????
?????? // 返回結(jié)果
?????? String tempNum = "";
?????? for (int i = 0; i < nums.length; i++) {
?????????? tempNum += nums[i];
?????? }
??????
?????? return (tempNum);
??? }
}
總結(jié)
以上是生活随笔為你收集整理的java实现中文数字与阿拉伯数字互相转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018贵州省大学生程序设计竞赛参赛感言
- 下一篇: 浅谈JAVA