将金额转成大写金额

  • Post author:
  • Post category:其他


import java.util.ArrayList;
import java.util.List;

public class DirectMemoryOOM {
    private static String hanDigiStr[] = new String[] { "零", "壹", "贰", "叁", "肆", "伍",
            "陆", "柒", "捌", "玖" };
    private static String hanDiviStr[] = new String[] { "", "拾", "佰", "仟", "万", "拾",
            "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰",
            "仟", "万", "拾", "佰", "仟" };

    public static void main(String[] args) throws Exception {
        int val = 20405;
        handlerValue(val);
    }
    private static void handlerValue(int val) {
        List<Integer> list = new ArrayList<>();
        while(val!=0){
            list.add(val%10);
            val/=10;
        }
        String res = "";
        int j;
        for (int i = list.size()-1; i >=0 ; i--) {
            Integer num = list.get(i);
            if(num!=0){
                res+= hanDigiStr[num]+hanDiviStr[i];
            }else{
                j=i;
                while(j>=0){
                    if(list.get(j)==num){
                        j--;
                        continue;
                    }
                    break;
                }
                if(j==-1) break;
                res+=hanDigiStr[num];
                i=j+1;
            }
        }
        System.out.println(res);
    }
}



版权声明:本文为jiyukun1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。