java數字轉漢字大寫(全)
阿新 • • 發佈:2018-12-17
- public class MoneyUtil {
- public static String[] chineseDigits = new String[] { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"};
- /**
- * 把金額轉換為漢字表示的數量,小數點後四捨五入保留兩位
- * @param amount
- * @return
- */
- public static String amountToChinese(double amount) {
- if(amount > 99999999999999.99 || amount < -99999999999999.99)
- throw new IllegalArgumentException("引數值超出允許範圍 (-99999999999999.99 ~ 99999999999999.99)!");
- boolean negative = false;
- if(amount < 0) {
- negative = true;
- amount = amount * (-1);
- }
- long temp = Math.round(amount * 100);
- int numFen = (int)(temp % 10); // 分
- temp = temp / 10;
- int numJiao = (int)(temp % 10); //角
- temp = temp / 10;
- //temp 目前是金額的整數部分
- int[] parts = new int[20]; // 其中的元素是把原來金額整數部分分割為值在 0~9999 之間的數的各個部分
- int numParts = 0; // 記錄把原來金額整數部分分割為了幾個部分(每部分都在 0~9999 之間)
- for(int i=0; ; i++) {
- if(temp ==0)
- break;
- int part = (int)(temp % 10000);
- parts[i] = part;
- numParts ++;
- temp = temp / 10000;
- }
- boolean beforeWanIsZero = true; // 標誌“萬”下面一級是不是 0
- String chineseStr = "";
- for(int i=0; i<numParts; i++) {
- String partChinese = partTranslate(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) // 如果“萬”對應的 part 為 0,而“萬”下面一級不為 0,則不加“萬”,而加“零”
- chineseStr = "零" + chineseStr;
- else {
- if(parts[i-1] < 1000 && parts[i-1] > 0) // 如果"萬"的部分不為 0, 而"萬"前面的部分小於 1000 大於 0, 則萬後面應該跟“零”
- chineseStr = "零" + chineseStr;
- chineseStr = "萬" + chineseStr;
- }
- }
- }
- chineseStr = partChinese + chineseStr;
- }
- if("".equals(chineseStr)) // 整數部分為 0, 則表達為"零元"
- chineseStr = chineseDigits[0];
- else if(negative) // 整數部分不為 0, 並且原金額為負數
- chineseStr = "負" + chineseStr;
- chineseStr = chineseStr + "元";
- if(numFen == 0 && numJiao == 0) {
- chineseStr = chineseStr + "整";
- }
- else if(numFen == 0) { // 0 分,角數不為 0
- chineseStr = chineseStr + chineseDigits[numJiao] + "角";
- }
- else { // “分”數不為 0
- if(numJiao == 0)
- chineseStr = chineseStr + "零" + chineseDigits[numFen] + "分";
- else
- chineseStr = chineseStr + chineseDigits[numJiao] + "角" + chineseDigits[numFen] + "分";
- }
- return chineseStr;
- }
- /**
- * 把一個 0~9999 之間的整數轉換為漢字的字串,如果是 0 則返回 ""
- * @param amountPart
- * @return
- */
- private static String partTranslate(int amountPart) {
- if(amountPart < 0 || amountPart > 10000) {
- throw new IllegalArgumentException("引數必須是大於等於 0,小於 10000 的整數!");
- }
- String[] units = new String[] {"", "拾", "佰", "仟"};
- int temp = amountPart;
- String amountStr = new Integer(amountPart).toString();
- int amountStrLength = amountStr.length();
- boolean lastIsZero = true; //在從低位往高位迴圈時,記錄上一位數字是不是 0
- String chineseStr = "";
- for(int i=0; i<amountStrLength; i++) {
- if(temp == 0) // 高位已無資料
- break;
- int digit = temp % 10;
- if(digit == 0) { // 取到的數字為 0
- if(!lastIsZero) //前一個數字不是 0,則在當前漢字串前加“零”字;
- chineseStr = "零" + chineseStr;
- lastIsZero = true;
- }
- else { // 取到的數字不是 0
- chineseStr = chineseDigits[digit] + units[i] + chineseStr;
- lastIsZero = false;
- }
- temp = temp / 10;
- }
- return chineseStr;
- }
- public static void main(String[] args) {
- if(args.length == 0) {
- System.out.println("轉換演示:");
- System.out.println("-------------------------");
- System.out.println("25000000000005.999: " + amountToChinese(25000000000005.999));
- System.out.println("45689263.626: " + amountToChinese(45689263.626));
- System.out.println("0.69457: " + amountToChinese(0.69457));
- System.out.println("253.0: " + amountToChinese(253.0));
- System.out.println("0: " + amountToChinese(0));
- System.out.println("-------------------------");
- System.out.println("999: " + amountToChinese(999));
- //System.out.println(Long.MAX_VALUE);
- //System.out.println(Long.MIN_VALUE);
- }
- else {
- System.out.println("轉換結果:");
- System.out.println(args[0] + ": " + amountToChinese(Double.parseDouble(args[0])));
- }
- }
- }