java数字⼤⼩写转换_阿拉伯数字⼤⼩写转换java⼯具
ic.utils;泰安住宿
教师介绍DecimalFormat;
public class MoneyUtil {生甘草的功效与作用
/** ⼤写数字 */ private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; /** 整数部分的单位*/ private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" }; /** ⼩数部分的单位 */ private static final String[] DUNIT = { "⾓", "分", "厘" };
public static String toChine(Double amount) { return toChine(amount, "1"); }
public static String toChine(Double amount, String currency) { DecimalFormat df = new DecimalFormat("###,##0.00"); String str = df.format(amount); return strToChine(str, currency); }
public static String getCurrencySymbol(String currency) { if (currency.equalsIgnoreCa("1")) { return "¥"; } el if (currency.equalsIgnoreCa("2")) { return "$"; } el if (currency.equalsIgnoreCa("3")
) { return "€"; } el if (currency.equalsIgnoreCa("4")) { return "£"; } el { return "¥"; } }
public static String getCurrencyUnit(String unit, String currency) { if (unit.equalsIgnoreCa(IUNIT[0])) { if马援铜柱
(currency.equalsIgnoreCa("4")) { return "镑"; } el { return IUNIT[0]; } } el { return unit; } }
/** * 得到⼤写⾦额。 */ public static String strToChine(String str) { return strToChine(str, "1"); }
/** * 得到不同币种的⼤写⾦额。 */ public static String strToChine(String str, String currency) { DecimalFormat df = DecimalFormat("00.00"); str = placeAll(",", "");// 去掉"," 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); } el if (str.indexOf(".") == 0) { integerStr = ""; decimalStr = str.substring(1); } el { integerStr = str; decimalStr = ""; } // integ
erStr去掉⾸0,不必去掉decimalStr的尾0(超出部分舍去) if
(!integerStr.equals("")) { integerStr = String(Long.parLong(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 getChineInteger(integers, isMust5, currency) + getChineDecimal(decimals); }
平行四边形的特征/** * 整数部分和⼩数部分转换为数组,从⾼位⾄低位 */ private static int[] toArray(String number) { int[] array = new
金银箔
int[number.length()]; for (int i = 0; i < number.length(); i++) { array[i] = Integer.parInt(number.substring(i, i + 1)); }
return array; }
起起落落/
** * 得到中⽂⾦额的整数部分。 */ private static String getChineInteger(int[] integers, boolean isMust5, String currency) { StringBuffer chineInteger = 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]; el if ((length - i) == 9)// 亿(必填) key = IUNIT[8]; el if ((length - i) == 5 && isMust5)// 万(不必填) key = IUNIT[4]; el if ((length - i) == 1) {// 元(必填)
key = getCurrencyUnit(IUNIT[0], currency); } // 0遇⾮0时补零,不包含最后⼀位 if ((length - i) > 1 && integers[i + 1] != 0) key += NUMBERS[0]; } chineInteger.append(integers[i] == 0 ? key : (NUMBERS[integers[i]] +
getCurrencyUnit( IUNIT[length - i - 1], currency))); } String(); }
private static String getChineInteger(int[] integers, boolean isMust5) { return getChineInteger(integers, isMust5, "1"); }
/** * 得到中⽂⾦额的⼩数部分。 */ private static String getChineDecimal(int[] decimals) { StringBuffer chineDecimal = new StringBuffer(""); for (int i = 0; i < decimals.length; i++) { // 舍去
青蛙跳跳
3位⼩数之后的 if (i == 3) break;
chineDecimal.append(decimals[i] == 0 ? "" : (NUMBERS[decimals[i]] + DUNIT[i])); } String(); }
/** * 判断第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); } el { subInteger = integerStr.substring(0, length - 4); } return Integer.parInt(subInteger) > 0; } el { return fal; } }
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.strToChine(number)); number = "1234567890123456.123"; System.out.println(number + " " + MoneyUtil.strToChine(nu
mber)); number = "0.0798"; System.out.println(number + " " + MoneyUtil.strToChine(number)); number = "10,001,000.09";
System.out.println(number + " " + MoneyUtil.strToChine(number)); number = "01.107700";
System.out.println(number + " " + MoneyUtil.strToChine(number));
double d = 12345678901234.12; System.out.Chine(d)); }
}