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