java算法面试题:金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。

package com.swift;

public class RenMingBi {

    private static final char[] data = new char[]{'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
    private static final char[] units = new char[]{'元','拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟','兆','拾','佰','仟','京','拾','佰','仟','垓'};

    public static void main(String[] args) {
        System.out.println(convert(7777777777777777777l));
        System.out.println(convert(9223372036854775807l));
        System.out.println(Long.MAX_VALUE);
    }

    public static String convert(long money) {
        StringBuffer sbf = new StringBuffer();
        int unit = 0;
        while (money != 0) {
            sbf.insert(0, units[unit++]);
            int number = (int) (money % 10);
            sbf.insert(0, data[number]);
            money /= 10;
        }

        return sbf.toString();
    }
}

 

posted @ 2018-01-29 12:31  Advancing-Swift  阅读(650)  评论(0编辑  收藏  举报