您好,登錄后才能下訂單哦!
本文實例為大家分享了java金額數字轉中文工具類的具體代碼,供大家參考,具體內容如下
java金額數字轉中文工具類ConvertNum.java
package light.mvc.utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * 金額數字轉中文工具類 * * @author ardo * */ public class ConvertNum { /** * 把金額阿拉伯數字轉換為漢字表示,小數點后四舍五入保留兩位 * 還有一種方法可以在轉換的過程中不考慮連續0的情況,然后對最終的結果進行一次遍歷合并連續的零 */ public static String[] ChineseNum = new String[] { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" }; public static String NumToChinese(double num) { if (num > 99999999999999.99 || num < -99999999999999.99) throw new IllegalArgumentException( "參數值超出允許范圍 (-99999999999999.99 ~ 99999999999999.99)!"); boolean negative = false;// 正負標號 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; // 此時temp只包含整數部分 int[] parts = new int[20];// 將金額整數部分分為在0-9999之間數的各個部分 int numParts = 0;// 記錄把原來金額整數部分分割為幾個部分 for (int i = 0;; i++) { if (temp == 0) break; int part = (int) (temp % 10000); parts[i] = part; temp = temp / 10000; numParts++; } boolean beforeWanIsZero = true;// 標志位,記錄萬的下一級是否為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)// 如果“萬”對應的 // 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 = ChineseNum[0]; else if (negative)// 整數部分部位0,但是為負數 chineseStr = "負" + 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; } // 轉換拆分后的每個部分,0-9999之間 public static String partConvert(int partNum) { if (partNum < 0 || partNum > 10000) { throw new IllegalArgumentException("參數必須是大于等于0或小于10000的整數"); } 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)// 高位無數字 break; int digit = temp % 10; if (digit == 0) { if (!lastIsZero)// 如果前一個數字不是0則在當前漢字串前加零 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("請輸入金額數字,-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("其他測試:"); 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)); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。