1 /**
2 * 金额转换工具
3 * @Author yinjianbo
4 * @Description TODO
5 * @Date 2019-12-23
6 */
7 public class MoneyUpperUntil {
8
9 private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分";
10 private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";
11 private static final Double MAX_VALUE = 9999999999999.99D;
12
13 /**
14 * 金额转换中文大写
15 * @author yinjianbo
16 * @param
17 * @return
18 */
19 public static String change(double v) {
20 if (v < 0 || v > MAX_VALUE){
21 return "参数非法!";
22 }
23 long l = Math.round(v * 100);
24 if (l == 0){
25 return "零元整";
26 }
27 String strValue = l + "";
28 // i用来控制数
29 int i = 0;
30 // j用来控制单位
31 int j = UNIT.length() - strValue.length();
32 String rs = "";
33 boolean isZero = false;
34 for (; i < strValue.length(); i++, j++) {
35 char ch = strValue.charAt(i);
36 if (ch == '0') {
37 isZero = true;
38 if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') {
39 rs = rs + UNIT.charAt(j);
40 isZero = false;
41 }
42 } else {
43 if (isZero) {
44 rs = rs + "零";
45 isZero = false;
46 }
47 rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
48 }
49 }
50 if (!rs.endsWith("分")) {
51 rs = rs + "整";
52 }
53 rs = rs.replaceAll("亿万", "亿");
54 return rs;
55 }
56
57 public static void main(String[] args) {
58 System.out.println(change(12460));
59 }
60 }