Java如何將數(shù)字金額轉(zhuǎn)為大寫(xiě)中文金額

這篇文章主要介紹了Java如何將數(shù)字金額轉(zhuǎn)為大寫(xiě)中文金額,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)主營(yíng)鐵嶺縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app開(kāi)發(fā)定制,鐵嶺縣h5微信小程序定制開(kāi)發(fā)搭建,鐵嶺縣網(wǎng)站營(yíng)銷推廣歡迎鐵嶺縣等地區(qū)企業(yè)咨詢

前言:

輸入數(shù)字金額參數(shù),運(yùn)行程序得到其對(duì)應(yīng)的大寫(xiě)中文金額;例如:輸入— 12.56,輸出— 12.56:壹拾貳元伍角陸分;

奉上代碼:

/**
 *@Title: ConvertUpMoney 
 * @Description: 將數(shù)字金額轉(zhuǎn)為大寫(xiě)漢字金額
 * @date: 2019年6月18日 下午10:52:27
 */
public class ConvertUpMoney {
 
 //大寫(xiě)數(shù)字
 private static final String[] NUMBERS = {"零","壹","貳","叁","肆","伍","陸","柒","捌","玖"};
 // 整數(shù)部分的單位
 private static final String[] IUNIT = {"元","拾","佰","仟","萬(wàn)","拾","佰","仟","億","拾","佰","仟","萬(wàn)","拾","佰","仟"};
 //小數(shù)部分的單位
 private static final String[] DUNIT = {"角","分","厘"};
 
 //轉(zhuǎn)成中文的大寫(xiě)金額
 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è)置萬(wàn)單位
 int[] decimals = toIntArray(decimalStr);//小數(shù)部分?jǐn)?shù)字
 String result = getChineseInteger(integers,isWan)+getChineseDecimal(decimals);//返回最終的大寫(xiě)金額
 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)為大寫(xiě)的金額
 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)//萬(wàn)(億)
  key = IUNIT[4];
 else if((length - i) == 9) {//億
  key = IUNIT[8];
 }else if((length - i) == 5 && isWan) {//萬(wàn)
  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)為大寫(xiě)的金額
 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á)到【萬(wàn)】
 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)為大寫(xiě)中文金額”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

本文題目:Java如何將數(shù)字金額轉(zhuǎn)為大寫(xiě)中文金額
網(wǎng)站鏈接:http://www.muchs.cn/article28/gceijp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站排名、外貿(mào)網(wǎng)站建設(shè)ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)