您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java如何將數(shù)字金額轉(zhuǎn)為大寫中文金額,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
前言:
輸入數(shù)字金額參數(shù),運(yùn)行程序得到其對(duì)應(yīng)的大寫中文金額;例如:輸入— 12.56,輸出— 12.56:壹拾貳元伍角陸分;
奉上代碼:
/** *@Title: ConvertUpMoney * @Description: 將數(shù)字金額轉(zhuǎn)為大寫漢字金額 * @date: 2019年6月18日 下午10:52:27 */ public class ConvertUpMoney { //大寫數(shù)字 private static final String[] NUMBERS = {"零","壹","貳","叁","肆","伍","陸","柒","捌","玖"}; // 整數(shù)部分的單位 private static final String[] IUNIT = {"元","拾","佰","仟","萬","拾","佰","仟","億","拾","佰","仟","萬","拾","佰","仟"}; //小數(shù)部分的單位 private static final String[] DUNIT = {"角","分","厘"}; //轉(zhuǎn)成中文的大寫金額 public static String toChinese(String str) { //判斷輸入的金額字符串是否符合要求 if (StringUtils.isBlank(str) || !str.matches("(-)?[\\d]*(.)?[\\d]*")) { System.out.println("抱歉,請(qǐng)輸入數(shù)字!"); return str; } if("0".equals(str) || "0.00".equals(str) || "0.0".equals(str)) { return "零元"; } //判斷是否存在負(fù)號(hào)"-" boolean flag = false; if(str.startsWith("-")){ flag = true; str = str.replaceAll("-", ""); } str = str.replaceAll(",", "");//去掉"," String integerStr;//整數(shù)部分?jǐn)?shù)字 String decimalStr;//小數(shù)部分?jǐn)?shù)字 //初始化:分離整數(shù)部分和小數(shù)部分 if(str.indexOf(".")>0) { integerStr = str.substring(0,str.indexOf(".")); decimalStr = str.substring(str.indexOf(".")+1); }else if(str.indexOf(".")==0) { integerStr = ""; decimalStr = str.substring(1); }else { integerStr = str; decimalStr = ""; } //beyond超出計(jì)算能力,直接返回 if(integerStr.length()>IUNIT.length) { System.out.println(str+":超出計(jì)算能力"); return str; } int[] integers = toIntArray(integerStr);//整數(shù)部分?jǐn)?shù)字 //判斷整數(shù)部分是否存在輸入012的情況 if (integers.length>1 && integers[0] == 0) { System.out.println("抱歉,請(qǐng)輸入數(shù)字!"); if (flag) { str = "-"+str; } return str; } boolean isWan = isWan5(integerStr);//設(shè)置萬單位 int[] decimals = toIntArray(decimalStr);//小數(shù)部分?jǐn)?shù)字 String result = getChineseInteger(integers,isWan)+getChineseDecimal(decimals);//返回最終的大寫金額 if(flag){ return "負(fù)"+result;//如果是負(fù)數(shù),加上"負(fù)" }else{ return result; } } //將字符串轉(zhuǎn)為int數(shù)組 private static int[] toIntArray(String number) { int[] array = new int[number.length()]; for(int i = 0;i<number.length();i++) { array[i] = Integer.parseInt(number.substring(i,i+1)); } return array; } //將整數(shù)部分轉(zhuǎn)為大寫的金額 public static String getChineseInteger(int[] integers,boolean isWan) { StringBuffer chineseInteger = new StringBuffer(""); int length = integers.length; if (length == 1 && integers[0] == 0) { return ""; } for(int i=0;i<length;i++) { String key = ""; if(integers[i] == 0) { if((length - i) == 13)//萬(億) key = IUNIT[4]; else if((length - i) == 9) {//億 key = IUNIT[8]; }else if((length - i) == 5 && isWan) {//萬 key = IUNIT[4]; }else if((length - i) == 1) {//元 key = IUNIT[0]; } if((length - i)>1 && integers[i+1]!=0) { key += NUMBERS[0]; } } chineseInteger.append(integers[i]==0?key:(NUMBERS[integers[i]]+IUNIT[length - i -1])); } return chineseInteger.toString(); } //將小數(shù)部分轉(zhuǎn)為大寫的金額 private static String getChineseDecimal(int[] decimals) { StringBuffer chineseDecimal = new StringBuffer(""); for(int i = 0;i<decimals.length;i++) { if(i == 3) { break; } chineseDecimal.append(decimals[i]==0?"":(NUMBERS[decimals[i]]+DUNIT[i])); } return chineseDecimal.toString(); } //判斷當(dāng)前整數(shù)部分是否已經(jīng)是達(dá)到【萬】 private static boolean isWan5(String integerStr) { int length = integerStr.length(); if(length > 4) { String subInteger = ""; if(length > 8) { subInteger = integerStr.substring(length- 8,length -4); }else { subInteger = integerStr.substring(0,length - 4); } return Integer.parseInt(subInteger) > 0; }else { return false; } } //Test public static void main(String[] args) { String number = "12.56"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "1234567890563886.123"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "1600"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "156,0"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "-156,0"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "0.12"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "0.0"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "01.12"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "0125"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "-0125"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); number = "sdw5655"; System.out.println(number+": "+ConvertUpMoney.toChinese(number)); System.out.println(null+": "+ConvertUpMoney.toChinese(null)); } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java如何將數(shù)字金額轉(zhuǎn)為大寫中文金額”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。