标签:
1 package sfbc; 2 3 import java.text.DecimalFormat; 4 import java.util.HashMap; 5 import java.util.Map; 6 /** 7 * 金额转换,阿拉伯数字的金额转换成中国传统输出。 8 * @author trfizeng 9 * 10 */ 11 public class FormateMoneyCN { 12 // 阿拉伯数字对应的大写汉字 13 public static final Map<String, String> NUMBER_CN = new HashMap<String, String>(); 14 // 大写单位 15 public static final String[] UNIT_UP_CN = new String[] { "圆", "拾", "佰", 16 "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" }; 17 // 小数点后的大写单位 18 public static final String[] UNIT_DOWN_CN = new String[] { "角", "分" }; 19 // 符号 20 public static final Map<String, String> SINGLE = new HashMap<String, String>(); 21 static { 22 NUMBER_CN.put(0 + "", "零"); 23 NUMBER_CN.put(1 + "", "壹"); 24 NUMBER_CN.put(2 + "", "贰"); 25 NUMBER_CN.put(3 + "", "叁"); 26 NUMBER_CN.put(4 + "", "肆"); 27 NUMBER_CN.put(5 + "", "伍"); 28 NUMBER_CN.put(6 + "", "陆"); 29 NUMBER_CN.put(7 + "", "柒"); 30 NUMBER_CN.put(8 + "", "捌"); 31 NUMBER_CN.put(9 + "", "玖"); 32 33 SINGLE.put("-", "负"); 34 SINGLE.put("", ""); 35 SINGLE.put("+", ""); 36 } 37 38 public static String formateNumberToCn(double number) { 39 String tFormateNumber = formateNumber(number); 40 return numberToCn(tFormateNumber); 41 } 42 43 // 格式化数据为小数点后面2位 44 public static String formateNumber(double number) { 45 DecimalFormat df = new DecimalFormat("#.##"); 46 return df.format(number); 47 } 48 49 // 把阿拉伯数据转化成中国大写 50 public static String numberToCn(String number) { 51 StringBuffer sb = new StringBuffer(); 52 // 得到小数点位置 53 int point = number.indexOf("."); 54 // 提取符号 55 String single = SINGLE 56 .get(number.substring(0, number.indexOf("-") + 1)); 57 // 无符号的数字 58 number = number.substring(number.indexOf("-") + 1); 59 point = number.indexOf("."); 60 // 整数部分 61 char[] upNumber = point > 0 ? number.substring(0, point).toCharArray() 62 : number.toCharArray(); 63 // 用来控制连续出现多个零的情况 64 char tUpNumber = ‘ ‘; 65 for (int i = upNumber.length; i > 0; i--) { 66 // 阿拉伯数字对应的大写汉字 67 String tN = tUpNumber == ‘0‘ && upNumber[upNumber.length - i] == tUpNumber ? "" 68 : NUMBER_CN.get(upNumber[upNumber.length - i] + ""); 69 if (!"".equals(tN)) { 70 // 如果是零后面就没单位 71 sb.append(tN + ("零".equals(tN) ? "" : UNIT_UP_CN[i - 1])); 72 } 73 tUpNumber = upNumber[upNumber.length - i]; 74 } 75 // 去除整数部分最后的零 76 if ("零".equals(sb.substring(sb.length() - 1))) { 77 sb.deleteCharAt(sb.length() - 1); 78 } 79 // 如果有小数 80 if (point > 0) { 81 // 小数部分 82 char[] downNumber = number.substring(point + 1).toCharArray(); 83 for (int i = 0; i < downNumber.length; i++) { 84 sb.append(NUMBER_CN.get(downNumber[i] + "") 85 + (downNumber[i] == ‘0‘ ? "" : UNIT_DOWN_CN[i])); 86 } 87 // 去除开头的零 88 if ("零".equals(sb.substring(0, 1))) { 89 sb.deleteCharAt(0); 90 } 91 } else { 92 // 没小数就是整 93 sb.append("整"); 94 } 95 return single + sb.toString(); 96 } 97 98 public static void main(String[] args) { 99 double number1 = 600073000.034; 100 double number2 = 600073000.036; 101 double number3 = 600073000; 102 System.out.println("\"" + number1 + "\":" + formateNumberToCn(number1)); 103 System.out.println("\"" + number2 + "\":" + formateNumberToCn(number2)); 104 System.out.println("\"" + number3 + "\":" + formateNumberToCn(number3)); 105 } 106 }
"6.00073000034E8":陆亿零柒万叁仟零叁分
"6.00073000036E8":陆亿零柒万叁仟零肆分
"6.00073E8":陆亿零柒万叁仟整
标签:
原文地址:http://www.cnblogs.com/trfizeng/p/4307679.html