赞
踩
/** * 金额转英文 */ public class MoneyUtil { public static String parse(String str) { int z = str.indexOf("."); // 取小数点位置 String lstr = "", rstr = ""; if (z > -1) { // 看是否有小数,如果有,则分别取左边和右边 lstr = str.substring(0, z); rstr = str.substring(z + 1); } else // 否则就是全部 lstr = str; String lstrrev = reverse(lstr); //对左边的字串取反 String[] a = new String[5]; // 定义5个字串变量来存放解析出来的叁位一组的字串 switch (lstrrev.length() % 3) { case 1: lstrrev += "00"; break; case 2: lstrrev += "0"; break; } String lm = ""; // 用来存放转换後的整数部分 for (int i = 0; i < lstrrev.length() / 3; i++) { a[i] = reverse(lstrrev.substring(3 * i, 3 * i + 3)); // 截取第一个叁位 // 用来避免这种情况:1000000 = one million thousand only if (!a[i].equals("000")) { if (i != 0) lm = transThree(a[i]) + " " + parseMore(String.valueOf(i)) + " " + lm; // 加: thousand、million、billion else lm = transThree(a[i]); // 防止i=0时, 在多加两个空格. } else lm += transThree(a[i]); } String xs = ""; // 用来存放转换後小数部分 if (z > -1) xs = "AND CENTS " + transTwo(rstr) + " "; // 小数部分存在时转换小数 return lm.trim() + " " + xs + "ONLY"; } private static String parseFirst(String str) { String[] a = new String[]{"", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"}; return a[Integer.parseInt(str.substring(str.length() - 1))]; } private static String parseTeen(String str) { String[] a = new String[]{"TEN", "ELEVEN", "TWELEVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"}; return a[Integer.parseInt(str) - 10]; } private static String parseTen(String str) { String[] a = new String[]{"TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"}; return a[Integer.parseInt(str.substring(0, 1)) - 1]; } // 两位 private static String transTwo(String str) { String value = ""; // 判断位数 if (str.length() > 2) { str = str.substring(0, 2); } else if (str.length() < 2) { str = "0" + str; } if(str.startsWith("0")){ // 07 - seven 是否小於10 value = parseFirst(str); }else if(str.startsWith("1")){ // 17 seventeen 是否在10和20之间 value = parseTeen(str); }else if (str.endsWith("0")) { // 是否在10与100之间的能被10整除的数 value = parseTen(str); }else { value = parseTen(str) + " " + parseFirst(str); } return value; } private static String parseMore(String str) { String[] a = new String[] { "", "THOUSAND", "MILLION", "BILLION" }; return a[Integer.parseInt(str)]; } // 制作叁位的数 private static String transThree(String str) { String value = ""; if (str.startsWith("0")) { // 是否小於100 value = transTwo(str.substring(1)); }else if (str.substring(1).equals("00")) { // 是否被100整除 value = parseFirst(str.substring(0, 1)) + " HUNDRED"; }else{ value = parseFirst(str.substring(0, 1)) + " HUNDRED AND " + transTwo(str.substring(1)); } return value; } private static String reverse(String str) { char[] aChr = str.toCharArray(); StringBuffer tmp = new StringBuffer(); for (int i = aChr.length - 1; i >= 0; i--) { tmp.append(aChr[i]); } return tmp.toString(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。