数字转大写钱币

 1 public class Tool {
 2 
 3     private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分";
 4     private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";
 5     private static final double MAX_VALUE = 9999999999999.99D;
 6     public static String change(double v) {
 7         if (v < 0 || v > MAX_VALUE){
 8             return "参数非法!";
 9         }
10         long lo = Math.round(v * 100);
11         if (lo == 0){
12             return "零元整";
13         }
14         String strValue = lo + "";
15         // i用来控制数
16         int i = 0;
17         // j用来控制单位
18         int j = UNIT.length() - strValue.length();
19         String rs = "";
20         boolean isZero = false;
21         for (; i < strValue.length(); i++, j++) {
22             char ch = strValue.charAt(i);
23             if (ch == '0') {
24                 isZero = true;
25                 if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') {
26                     rs = rs + UNIT.charAt(j);
27                     isZero = false;
28                 }
29             } else {
30                 if (isZero) {
31                     rs = rs + "零";
32                     isZero = false;
33                 }
34                 rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
35             }
36         }
37         if (!rs.endsWith("分") && !rs.endsWith("角")) {
38             rs = rs + "整";
39         }
40         rs = rs.replaceAll("亿万", "亿");
41         return rs;
42     }
43 
44     public static void main(String[] args){
45         System.out.println(Tool.change(70005001.0));
46 //        System.out.println(Tool.change(12356789.9845));
47     }
48 }  

 

posted @ 2017-05-17 18:00  刘译繁liuyifan  阅读(392)  评论(0编辑  收藏  举报