首页 > 代码库 > 爪哇国新游记之二十----将数字转换成汉子大写形式

爪哇国新游记之二十----将数字转换成汉子大写形式

/** * 辅助类 * 用于记载字符和位置 * */class CharPos{    char c;    int pos;        public CharPos(char c,int pos){        this.c=c;        this.pos=pos;    }}
/** * 将数字转化成汉字大写形式 *  */public class CnNumMaker {    private String integerPart;// 整数部分    private String decimalPart;// 小数部分    private Stack<CharPos> stack;// 存放整数部分的栈    private char[] arr;// 存放万基单位的数组    public CnNumMaker(String strIn) {        // 分离成整数部分和小数部分        if (strIn.contains(".")) {            // 包含小数点            int dotIndex = strIn.indexOf(".");            integerPart = strIn.substring(0, dotIndex);            decimalPart = strIn.substring(dotIndex + 1);        } else {            // 不包含小数点            integerPart = strIn;        }        // 初始化栈        int length = integerPart.length();        stack = new Stack<CharPos>(CharPos.class, length);        arr=new char[length];        // 向栈中填值        for (int i = 0; i < length; i++) {            char c = integerPart.charAt(i);            int pos = length - i - 1;            if (c == ‘0‘) {                if (stack.isEmpty()) {                    stack.push(new CharPos(c, pos));                } else {                    CharPos top = stack.peek();                    if (top.c != ‘0‘) {                        stack.push(new CharPos(c, pos));                    }                }            } else {                stack.push(new CharPos(c, pos));            }            // 插入逗号,作为单位            if (pos % 4 == 0 && pos != 0) {                arr[pos]=‘,‘;            }        }                // 清除尾后的零        CharPos top=stack.peek();                while(top.c==‘0‘){            stack.pop();                                    if(stack.isEmpty()==false){                top=stack.peek();            }else{                stack.push(top);                break;            }        }    }        public String toString(){        StringBuilder sb=new StringBuilder();                // 整数部分处理        if(Long.parseLong(integerPart)==0){            sb.append("零");        }else{            while(stack.isEmpty()==false){                CharPos poped=stack.pop();                                int t1=poped.pos/4;                int t2=t1*4;                                                if(arr[t2]!=0 && ‘0‘!=poped.c /*&& poped.pos==t2*/){                    sb.insert(0, getWanUnit(t2));                    //arr[t2]=0;                }                                if(‘0‘==poped.c){                    sb.insert(0, ‘零‘);                }else{                                                                                sb.insert(0, getCnUpperNumber(poped.c)+getUnit(poped.pos));                }            }        }                // 小数部分处理        if(decimalPart!=null && decimalPart.length()>0){            sb.append("点");            for(int i=0;i<decimalPart.length();i++){                sb.append(getCnUpperNumber(decimalPart.charAt(i)));            }        }                return sb.toString();    }        // 得到数字的汉字大写    private static char getCnUpperNumber(char c){        final char[] Uppers = { ‘零‘, ‘壹‘, ‘贰‘, ‘叁‘, ‘肆‘, ‘伍‘, ‘陆‘,‘柒‘, ‘捌‘, ‘玖‘ };                String str=String.valueOf(c);           int number=Integer.parseInt(str);        return Uppers[number];    }        // 得到十百千这种单位    private static String getUnit(int pos){        final String[] Units = { "", "拾", "百", "仟"};        int mod=pos%4;        return Units[mod];    }        // 得到万亿兆这种单位    private static String getWanUnit(int pos){        final String[] Wans = { "", "万", "亿", "兆", "京", "垓", "秭", "穰", "沟", "涧", "正", "载"};        return Wans[pos/4];    }    public static void main(String[] args) {         //CnNumMaker c=new CnNumMaker("10000");        // c.toString();        String[] arr = {"0", "1", "10","100","1000", "10000","100000", "1000000",                "10000000", "100000000", "1000000000", "10123", "100123",                "1000123", "10012003", "1234567890.043243",                 "567891234567890.043243" ,"3243243344567890.043243" ,"24","108","9234","9087","9008","10001","100001","10000001","1000000001","10000000001","100000000001","1000000000001","10000000000001","10007600089001"};        for (String str : arr) {            try {                CnNumMaker c = new CnNumMaker(str);                System.out.println(str+"->"+c.toString());            } catch (Exception e) {                e.printStackTrace();                System.out.println("Error:" + str);            }        }    }}

输出:

0->1->10->壹拾100->壹百1000->壹仟10000->壹万100000->壹拾万1000000->壹百万10000000->壹仟万100000000->壹亿1000000000->壹拾亿10123->壹万零壹百贰拾叁100123->壹拾万零壹百贰拾叁1000123->壹百万零壹百贰拾叁10012003->壹仟万零壹万贰仟零叁1234567890.043243->壹拾亿贰亿叁仟万肆百万伍拾万陆万柒仟捌百玖拾点零肆叁贰肆叁567891234567890.043243->伍百兆陆拾兆柒兆捌仟亿玖百亿壹拾亿贰亿叁仟万肆百万伍拾万陆万柒仟捌百玖拾点零肆叁贰肆叁3243243344567890.043243->叁仟兆贰百兆肆拾兆叁兆贰仟亿肆百亿叁拾亿叁亿肆仟万肆百万伍拾万陆万柒仟捌百玖拾点零肆叁贰肆叁24->贰拾肆108->壹百零捌9234->玖仟贰百叁拾肆9087->玖仟零捌拾柒9008->玖仟零捌10001->壹万零壹100001->壹拾万零壹10000001->壹仟万零壹1000000001->壹拾亿零壹10000000001->壹百亿零壹100000000001->壹仟亿零壹1000000000001->壹兆零壹10000000000001->壹拾兆零壹10007600089001->壹拾兆零柒拾亿陆亿零捌万玖仟零壹

 

爪哇国新游记之二十----将数字转换成汉子大写形式