java金額數(shù)字轉(zhuǎn)中文工具類詳解
本文實(shí)例為大家分享了java金額數(shù)字轉(zhuǎn)中文工具類的具體代碼,供大家參考,具體內(nèi)容如下
java金額數(shù)字轉(zhuǎn)中文工具類ConvertNum.java
package light.mvc.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 金額數(shù)字轉(zhuǎn)中文工具類 * * @author ardo * */ public class ConvertNum { /** * 把金額阿拉伯?dāng)?shù)字轉(zhuǎn)換為漢字表示,小數(shù)點(diǎn)后四舍五入保留兩位 * 還有一種方法可以在轉(zhuǎn)換的過程中不考慮連續(xù)0的情況,然后對(duì)最終的結(jié)果進(jìn)行一次遍歷合并連續(xù)的零 */ public static String[] ChineseNum = new String[] { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" }; public static String NumToChinese(double num) { if (num > 99999999999999.99 || num < -99999999999999.99) throw new IllegalArgumentException( "參數(shù)值超出允許范圍 (-99999999999999.99 ~ 99999999999999.99)!"); boolean negative = false;// 正負(fù)標(biāo)號(hào) if (num < 0) { negative = true; num = num * (-1); } long temp = Math.round(num * 100); int numFen = (int) (temp % 10);// 分 temp = temp / 10; int numJiao = (int) (temp % 10);// 角 temp = temp / 10; // 此時(shí)temp只包含整數(shù)部分 int[] parts = new int[20];// 將金額整數(shù)部分分為在0-9999之間數(shù)的各個(gè)部分 int numParts = 0;// 記錄把原來金額整數(shù)部分分割為幾個(gè)部分 for (int i = 0;; i++) { if (temp == 0) break; int part = (int) (temp % 10000); parts[i] = part; temp = temp / 10000; numParts++; } boolean beforeWanIsZero = true;// 標(biāo)志位,記錄萬的下一級(jí)是否為0 String chineseStr = ""; for (int i = 0; i < numParts; i++) { String partChinese = partConvert(parts[i]); if (i % 2 == 0) { if ("".equals(partChinese)) beforeWanIsZero = true; else beforeWanIsZero = false; } if (i != 0) { if (i % 2 == 0)// 億的部分 chineseStr = "億" + chineseStr; else { if ("".equals(partChinese) && !beforeWanIsZero)// 如果“萬”對(duì)應(yīng)的 // part 為 // 0,而“萬”下面一級(jí)不為 // 0,則不加“萬”,而加“零” chineseStr = "零" + chineseStr; else { if (parts[i - 1] < 1000 && parts[i - 1] > 0)// 如果萬的部分不為0,而萬前面的部分小于1000大于0,則萬后面應(yīng)該跟零 chineseStr = "零" + chineseStr; chineseStr = "萬" + chineseStr; } } } chineseStr = partChinese + chineseStr; } if ("".equals(chineseStr))// 整數(shù)部分為0,則表示為零元 chineseStr = ChineseNum[0]; else if (negative)// 整數(shù)部分部位0,但是為負(fù)數(shù) chineseStr = "負(fù)" + chineseStr; chineseStr = chineseStr + "元"; if (numFen == 0 && numJiao == 0) { chineseStr = chineseStr + "整"; } else if (numFen == 0) {// 0分 chineseStr = chineseStr + ChineseNum[numJiao] + "角"; } else { if (numJiao == 0) chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分"; else chineseStr = chineseStr + ChineseNum[numJiao] + "角" + ChineseNum[numFen] + "分"; } return chineseStr; } // 轉(zhuǎn)換拆分后的每個(gè)部分,0-9999之間 public static String partConvert(int partNum) { if (partNum < 0 || partNum > 10000) { throw new IllegalArgumentException("參數(shù)必須是大于等于0或小于10000的整數(shù)"); } String[] units = new String[] { "", "拾", "佰", "仟" }; int temp = partNum; String partResult = new Integer(partNum).toString(); int partResultLength = partResult.length(); boolean lastIsZero = true;// 記錄上一位是否為0 String chineseStr = ""; for (int i = 0; i < partResultLength; i++) { if (temp == 0)// 高位無數(shù)字 break; int digit = temp % 10; if (digit == 0) { if (!lastIsZero)// 如果前一個(gè)數(shù)字不是0則在當(dāng)前漢字串前加零 chineseStr = "零" + chineseStr; lastIsZero = true; } else { chineseStr = ChineseNum[digit] + units[i] + chineseStr; lastIsZero = false; } temp = temp / 10; } return chineseStr; } public static void main(String args[]) { double num = 0; System.out.println("請(qǐng)輸入金額數(shù)字,-1退出"); try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); num = Double.parseDouble(br.readLine()); } catch (IOException e) { System.out.println(e.toString()); } while (num != -1) { System.out.println(num + NumToChinese(num)); try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); num = Double.parseDouble(br.readLine()); } catch (IOException e) { System.out.println(e.toString()); } } System.out.println("其他測(cè)試:"); System.out.println("100120: " + NumToChinese(100120)); System.out.println("25000000000005.999: " + NumToChinese(25000000000005.999)); System.out.println("45689263.626: " + NumToChinese(45689263.626)); System.out.println("0.69457: " + NumToChinese(0.69457)); System.out.println("253.0: " + NumToChinese(253.0)); System.out.println("0: " + NumToChinese(0)); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ThreadLocal原理介紹及應(yīng)用場(chǎng)景
本文詳細(xì)講解了ThreadLocal原理介紹及應(yīng)用場(chǎng)景,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12Java Map如何根據(jù)key取value以及不指定key取出所有的value
這篇文章主要介紹了Java Map如何根據(jù)key取value以及不指定key取出所有的value,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09SpringBoot在自定義類中調(diào)用service層等Spring其他層操作
這篇文章主要介紹了SpringBoot在自定義類中調(diào)用service層等Spring其他層操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06RocketMQ?producer容錯(cuò)機(jī)制源碼解析
這篇文章主要為大家介紹了RocketMQ?producer容錯(cuò)機(jī)制源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03解決@DateTimeFormat格式化時(shí)間出錯(cuò)問題
這篇文章主要介紹了解決@DateTimeFormat格式化時(shí)間出錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12關(guān)于@MapperScan和@ComponentScan的使用問題
文章介紹了在使用`@MapperScan`和`@ComponentScan`時(shí)可能會(huì)遇到的包掃描沖突問題,并提供了解決方法,同時(shí),還詳細(xì)解釋了`@MapperScan`和`@ComponentScan`的功能和使用場(chǎng)景2025-01-01