java 把数字转换为中文读法 例如:123->一百二十三

程序中是把 字符串形式的数字 转换 为中文读法,需要转换int型的话,可以先使用java.lang.String.valueOf(int i)转为字符串,不支持浮点数的转换。

 

package com.dapeng.contest1; public class NumberTransfrom { private String[] unms = new String[] { "零","一","二","三","四","五","六","七","八","九" }; private String[] digits = new String[] { "","十","百","千" }; private String[] units = new String[] { "","万","亿","万亿" }; public NumberTransfrom() { } public NumberTransfrom( String[] nums, String[] digits, String[] units ) { this.unms = nums; this.digits = digits; this.units = units; } public void setUnits( String[] units ) { this.units = units; } public void setNums( String[] nums ) { this.unms = nums; } public void setDigits( String[] digits ) { this.digits = digits; } //检查字符串s是否全为数字 public boolean checkIsNumbers( String x ) { if (null == x) { return false; } for( Character c: x.toCharArray() ) { if(c.compareTo('0')<0 || c.compareTo('9')>0) { return false; } } return true; } public String transfrom( String x ) { if( null==x ) { return "您输入的字符串地址为null!"; } if( 0==x.length() ) { return "您输入的字符串长度为0,请输入要转换的数字!"; } if( false==checkIsNumbers(x) ) { return "您输入的字符不都为数字,无法转换!"; } if( x.length()>16 ) { return "您输入的字符串长度大于16,无法转换!"; } //去除字符串首部的0,例如:0010->10 int fm; for( fm=0 ; fm1,2345,6789 String result = ""; int p = 0; int m = x.length()%4; int k = ( m>0 ? x.length()/4+1 : x.length()/4 ); //从最左边的那组开始,向右循环 for( int i=k; i>0; i-- ) { int len=4; if ( i==k && m!=0 )//当i为最左边的那组时,组长度可能有变化 { len=m; } String s = x.substring( p, p+len ); int le = s.length(); for( int j=0; j0 && !result.endsWith(unms[0])) {//加零的条件:不为最后一位 && 下一位数字大于0 && 以前没有加过“零” result += unms[0]; } } else { if( !(n==1 && (result.endsWith(unms[0])||result.length()==0) && j==le-2) ) {//处理1013一千零"十三",1113 一千一百"一十三" result += unms[n]; } result += digits[le-j-1]; } } if ( 0!=java.lang.Integer.parseInt(s) )//如果这组数字不全是 0 ,则加上单位:万,亿,万亿 { result += units[i-1]; } p += len; } return result; } }

 

package com.dapeng.contest1; public class Client { public static void main(String[] args) { NumberTransfrom numTrans = new NumberTransfrom(); System.out.println( "null/t/t/t"+numTrans.transfrom(null) ); System.out.println( "/"/"/t/t/t"+numTrans.transfrom("") ); System.out.println( "1234567/t/t/t"+numTrans.transfrom("1234567") ); System.out.println( "0001/t/t/t"+numTrans.transfrom("0001") ); System.out.println( "1001/t/t/t"+numTrans.transfrom("1001") ); System.out.println( "1100/t/t/t"+numTrans.transfrom("1100") ); System.out.println( "10100/t/t/t"+numTrans.transfrom("10100") ); System.out.println( "0010/t/t/t"+numTrans.transfrom("0010") ); System.out.println( "00000001/t/t"+numTrans.transfrom("00000001") ); System.out.println( "10000001/t/t"+numTrans.transfrom("10000001") ); System.out.println( "10000010/t/t"+numTrans.transfrom("10000010") ); System.out.println( "10000100/t/t"+numTrans.transfrom("10000100") ); System.out.println( "11000000/t/t"+numTrans.transfrom("11000000") ); System.out.println( "10100000/t/t"+numTrans.transfrom("10100000") ); System.out.println( "10010000/t/t"+numTrans.transfrom("10010000") ); System.out.println( "10001000/t/t"+numTrans.transfrom("10001000") ); System.out.println( "a124/t/t/t"+numTrans.transfrom("a124") ); System.out.println( "111112100113/t/t"+numTrans.transfrom("111112100113") ); System.out.println( "101140101031013/t/t"+numTrans.transfrom("101140101031013") ); System.out.println( "4444333322221111/t"+numTrans.transfrom("4444333322221111")); System.out.println( "54444333322221111/t"+numTrans.transfrom("54444333322221111")); System.out.println("---------------------------------------------------------"); numTrans.setNums( new String[] {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"} ); numTrans.setDigits( new String[] {"","拾","佰","仟"} ); numTrans.setUnits(new String[] { "","[万]","[亿]","[万亿]" }); System.out.println( "null/t/t/t"+numTrans.transfrom(null) ); System.out.println( "/"/"/t/t/t"+numTrans.transfrom("") ); System.out.println( "1234567/t/t/t"+numTrans.transfrom("1234567") ); System.out.println( "0001/t/t/t"+numTrans.transfrom("0001") ); System.out.println( "1001/t/t/t"+numTrans.transfrom("1001") ); System.out.println( "1100/t/t/t"+numTrans.transfrom("1100") ); System.out.println( "10100/t/t/t"+numTrans.transfrom("10100") ); System.out.println( "0010/t/t/t"+numTrans.transfrom("0010") ); System.out.println( "00000001/t/t"+numTrans.transfrom("00000001") ); System.out.println( "10000001/t/t"+numTrans.transfrom("10000001") ); System.out.println( "10000010/t/t"+numTrans.transfrom("10000010") ); System.out.println( "10000100/t/t"+numTrans.transfrom("10000100") ); System.out.println( "11000000/t/t"+numTrans.transfrom("11000000") ); System.out.println( "10100000/t/t"+numTrans.transfrom("10100000") ); System.out.println( "10010000/t/t"+numTrans.transfrom("10010000") ); System.out.println( "10001000/t/t"+numTrans.transfrom("10001000") ); System.out.println( "a124/t/t/t"+numTrans.transfrom("a124") ); System.out.println( "111112100113/t/t"+numTrans.transfrom("111112100113") ); System.out.println( "101140101031013/t/t"+numTrans.transfrom("101140101031013") ); System.out.println( "4444333322221111/t"+numTrans.transfrom("4444333322221111")); System.out.println( "54444333322221111/t"+numTrans.transfrom("54444333322221111")); } }

 

结果:

 

null			您输入的字符串地址为null!
""			您输入的字符串长度为0,请输入要转换的数字!
1234567			一百二十三万四千五百六十七
0001			一
1001			一千零一
1100			一千一百
10100			一万零一百
0010			十
00000001		一
10000001		一千万零一
10000010		一千万零十
10000100		一千万零一百
11000000		一千一百万
10100000		一千零十万
10010000		一千零一万
10001000		一千万一千
a124			您输入的字符不都为数字,无法转换!
111112100113		一千一百一十一亿一千二百一十万零一百一十三
101140101031013		一百零一万亿一千四百零一亿零一百零三万一千零十三
4444333322221111	四千四百四十四万亿三千三百三十三亿二千二百二十二万一千一百一十一
54444333322221111	您输入的字符串长度大于16,无法转换!
---------------------------------------------------------
null			您输入的字符串地址为null!
""			您输入的字符串长度为0,请输入要转换的数字!
1234567			壹佰贰拾叁[万]肆仟伍佰陆拾柒
0001			壹
1001			壹仟零壹
1100			壹仟壹佰
10100			壹[万]零壹佰
0010			拾
00000001		壹
10000001		壹仟[万]零壹
10000010		壹仟[万]零拾
10000100		壹仟[万]零壹佰
11000000		壹仟壹佰[万]
10100000		壹仟零拾[万]
10010000		壹仟零壹[万]
10001000		壹仟[万]壹仟
a124			您输入的字符不都为数字,无法转换!
111112100113		壹仟壹佰壹拾壹[亿]壹仟贰佰壹拾[万]零壹佰壹拾叁
101140101031013		壹佰零壹[万亿]壹仟肆佰零壹[亿]零壹佰零叁[万]壹仟零拾叁
4444333322221111	肆仟肆佰肆拾肆[万亿]叁仟叁佰叁拾叁[亿]贰仟贰佰贰拾贰[万]壹仟壹佰壹拾壹
54444333322221111	您输入的字符串长度大于16,无法转换!

你可能感兴趣的:(java,学习)