阿拉伯數字大小寫轉換java工具


package org.centric.utils;

import java.text.DecimalFormat;

public class MoneyUtil {

 /** 大寫數字 */  private static final String[] NUMBERS = { "零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖" };  /** 整數部分的單位 */  private static final String[] IUNIT = { "元", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟", "萬", "拾", "佰",    "仟" };  /** 小數部分的單位 */  private static final String[] DUNIT = { "角", "分", "厘" };

 public static String toChinese(Double amount) {   return toChinese(amount, "1");  }

 public static String toChinese(Double amount, String currency) {   DecimalFormat df = new DecimalFormat("###,##0.00");   String str = df.format(amount);   return strToChinese(str, currency);  }

 public static String getCurrencySymbol(String currency) {   if (currency.equalsIgnoreCase("1")) {    return "¥";   } else if (currency.equalsIgnoreCase("2")) {    return "$";   } else if (currency.equalsIgnoreCase("3")) {    return "€";   } else if (currency.equalsIgnoreCase("4")) {    return "£";   } else {    return "¥";   }  }

 public static String getCurrencyUnit(String unit, String currency) {   if (unit.equalsIgnoreCase(IUNIT[0])) {    if (currency.equalsIgnoreCase("4")) {     return "鎊";    } else {     return IUNIT[0];    }   } else {    return unit;   }  }

 /**   * 得到大寫金額。   */  public static String strToChinese(String str) {   return strToChinese(str, "1");  }

 /**   * 得到不同幣種的大寫金額。   */  public static String strToChinese(String str, String currency) {   java.text.DecimalFormat df = new java.text.DecimalFormat("00.00");   str = str.replaceAll(",", "");// 去掉","   str = df.format(Double.valueOf(str));   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 = "";   }   // integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去)   if (!integerStr.equals("")) {    integerStr = Long.toString(Long.parseLong(integerStr));    if (integerStr.equals("0")) {     integerStr = "";    }   }   // overflow超出處理能力,直接返回   if (integerStr.length() > IUNIT.length) {    System.out.println(str + ":超出處理能力");    return str;   }

  int[] integers = toArray(integerStr);// 整數部分數字   boolean isMust5 = isMust5(integerStr);// 設置萬單位   int[] decimals = toArray(decimalStr);// 小數部分數字   return getChineseInteger(integers, isMust5, currency) + getChineseDecimal(decimals);  }

 /**   * 整數部分和小數部分轉換為數組,從高位至低位   */  private static int[] toArray(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;  }

 /**   * 得到中文金額的整數部分。   */  private static String getChineseInteger(int[] integers, boolean isMust5, String currency) {   StringBuffer chineseInteger = new StringBuffer("");   int length = integers.length;   for (int i = 0; i < length; i++) {    // 0出現在關鍵位置:1234(萬)5678(億)9012(萬)3456(元)    // 特殊情況:10(拾元、壹拾元、壹拾萬元、拾萬元)    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 && isMust5)// 萬(不必填)      key = IUNIT[4];     else if ((length - i) == 1) {// 元(必填)      key = getCurrencyUnit(IUNIT[0], currency);     }     // 0遇非0時補零,不包含最后一位     if ((length - i) > 1 && integers[i + 1] != 0)      key += NUMBERS[0];    }    chineseInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] + getCurrencyUnit(      IUNIT[length - i - 1], currency)));   }   return chineseInteger.toString();  }

 private static String getChineseInteger(int[] integers, boolean isMust5) {   return getChineseInteger(integers, isMust5, "1");  }

 /**   * 得到中文金額的小數部分。   */  private static String getChineseDecimal(int[] decimals) {   StringBuffer chineseDecimal = new StringBuffer("");   for (int i = 0; i < decimals.length; i++) {    // 舍去3位小數之后的    if (i == 3)     break;    chineseDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i]));   }   return chineseDecimal.toString();  }

 /**   * 判斷第5位數字的單位"萬"是否應加。   */  private static boolean isMust5(String integerStr) {   int length = integerStr.length();   if (length > 4) {    String subInteger = "";    if (length > 8) {     // 取得從低位數,第5到第8位的字串     subInteger = integerStr.substring(length - 8, length - 4);    } else {     subInteger = integerStr.substring(0, length - 4);    }    return Integer.parseInt(subInteger) > 0;   } else {    return false;   }  }

 public static void main(String[] args) {   String name = "xuhailiang";   if (name.indexOf('@') > 0)    name = name.substring(0, name.indexOf('@'));

  String number = "1.23";   System.out.println(number + " " + MoneyUtil.strToChinese(number));   number = "1234567890123456.123";   System.out.println(number + " " + MoneyUtil.strToChinese(number));   number = "0.0798";   System.out.println(number + " " + MoneyUtil.strToChinese(number));   number = "10,001,000.09";   System.out.println(number + " " + MoneyUtil.strToChinese(number));   number = "01.107700";   System.out.println(number + " " + MoneyUtil.strToChinese(number));

  double d = 12345678901234.12;   System.out.println(MoneyUtil.toChinese(d));  }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM