首先明确银行的输入的数字是字符串还是数值类型,因为要转换为中文,你需要事先定义位数读法,比如两位数是十,三位数是千,四位数是万,然后最终有一个基准,比如最大后面都是以亿计,然后计数以亿为标准,比如9位;以及你要定义数字读法,比如1为一,2为二等待。如果是数值类型,则采用除10方法,即不断除10,比如6666,则统计次数,6666%10=6,count+=1,为六,接下来(6666/10)%10=6,count+=1,此时count为2,你定义位数为2为十,则是六十。以此类推。字符串类型的和数值相似,此时你可以不用除10而以字符串的位数为数值位数。
//银行 数字转换老写中文案例 public class Bank { public static void main(String[] args) { String num="66666"; String chinaStr=""; String[] str = {"","十","百","千","万","十","百","千","亿", "十","百","千","万","兆"}; String[] subStr = Arrays.copyOf(str, num.length()); String[] chinaNumber = new String[num.length()]; for(int i =0;i<num.length();i++){ int numi=(Integer.parseInt(num.charAt(i)+"")); switch(numi){ case 1:chinaNumber[i]="壹"; break; case 2:chinaNumber[i]="贰"; break; case 3:chinaNumber[i]="叁"; break; case 4:chinaNumber[i]="肆"; break; case 5:chinaNumber[i]="伍"; break; case 6:chinaNumber[i]="陆"; break; case 7:chinaNumber[i]="柒"; break; case 8:chinaNumber[i]="捌"; break; case 9:chinaNumber[i]="玖"; break; case 0:chinaNumber[i]="零"; break; } } for(int i=0;i<num.length();i++){ //chinaStr+=num.charAt(i); chinaStr+=chinaNumber[i]; System.out.println(chinaStr); for(int j=subStr.length-1;j>0;j--){ if((j+i)==num.length()-1){ chinaStr+=subStr[j]; System.out.println(chinaStr); } } } }
}
本文还需改进.哪位大神有建议可以指导指导菜鸡~
努力让自己优秀.