java练习:金额转换,阿拉伯数字转换成中文传统形式
2015-09-05 12:39
656 查看
需求:金额转换,阿拉伯数字转换成中文传统形式 ,例如 101000001010 转为 壹仟零壹拾亿零壹仟零壹拾圆整
最终版:
有bug版
最终版:
import java.util.Scanner; public class Test { public static void main(String[] args) { long num = 101000001010l; String[] digit = {"零","壹","貳","叁","肆","伍","陆","柒","扒","玖"}; String[] unit = {"整","圆","拾","百","仟","万","拾","百","仟","亿", "拾","百","仟","万"}; while(true){ num = new Scanner(System.in).nextLong(); char[] chArr = Long.toString(num).toCharArray(); StringBuilder sb = new StringBuilder(""); for(int i = 0;i < chArr.length;i++){ String s = digit[Integer.parseInt(String.valueOf(chArr[i]))]; String s2 = unit[chArr.length-i]; sb.append(s).append(s2); } sb.append(unit[0]); String str = sb.toString(); str = change(str); System.out.println(str);} } private static String change(String str) { String s = str.replaceAll("零[仟百拾]", "零"); s = s.replaceAll("零+", "零").replaceAll("零亿", "亿").replaceAll("零万", "万"); s = s.replaceAll("零圆", "圆").replace("亿万", "亿"); return s; } }
有bug版
import java.util.Scanner; public class Test2 { public static void main(String[] args) { // TODO Auto-generated method stub long num = 101000001010l;//一千零一十亿零一千零一十圆整 num = 1010l;//一千零一十亿零一千零一十圆整 while(true){ System.out.println("输入数学数字:"); num = new Scanner(System.in).nextLong(); String[] unit = {"圆整","万","亿"}; int count = -1; StringBuilder sb = new StringBuilder(""); while(num>0){ count++; String s = ""; long n = num%10000;//取后四位 if(n < 10) s = "000"+n; else if(n < 100 ) s = "00"+n; else if(n < 1000) s = "0"+n; else s = s+n; num /= 10000; if(!s.equals("0000")) s = toNum(s)+unit[count];//返回的数字加单位 else s = toNum(s); sb.insert(0, s); // System.out.println(sb); } String chineseNum = sb.toString(); System.out.println(chineseNum); } } public static String toNum(String num){ String[] digit = {"零","壹","貳","叁","肆","伍","陆","柒","扒","玖"}; char[] ch = num.toCharArray();//字符串形式的四位数 int count = 0;//记录字符串含0的个数 int index = -1; while((index = num.indexOf("0",index+1))!= -1) count++; // System.out.println("字符串含0个数count = "+count); String numStr = "";//返回的中文数字 switch(count){ case 4: numStr = "零"; break; case 3: int temp = Integer.parseInt(num); if(temp<10) numStr = digit[ch[3]-'0']; else if(temp > 9 && temp < 101) numStr = digit[ch[2]-'0']+"拾"; else if(temp > 99 && temp < 1000) numStr = digit[ch[1]-'0'] + "百"; else numStr = digit[ch[0]-'0'] + "仟"; break; case 2: if(num.matches("[0][0][1-9][1-9]")){ numStr = numStr + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0'];; } else if(num.matches("[1-9][0][0][1-9]")){ numStr = numStr + digit[ch[0]-'0'] + "仟" +"零"+ digit[ch[3]-'0']; } else if(num.matches("[1-9][1-9][0][0]")){ numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百"; } else if(num.matches("[0][1-9][1-9][0]")){ numStr = numStr + digit[ch[1]-'0'] + "百"+ digit[ch[2]-'0']+"拾"; } else if(num.matches("[0][1-9][0][1-9]")){ numStr = numStr + digit[ch[1]-'0'] + "百"+"零"+ digit[ch[3]-'0']; } else if(num.matches("[1-9][0][1-9][0]")){ numStr = numStr + digit[ch[0]-'0'] + "仟"+"零"+ digit[ch[2]-'0']+"拾"; } break; case 1: if(num.matches("[0][1-9][1-9][1-9]")){ numStr = numStr + digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0']; } else if(num.matches("[1-9][0][1-9][1-9]")){ numStr = numStr + digit[ch[0]-'0'] + "仟" +"零"+ digit[ch[2]-'0']+"拾"+digit[ch[3]-'0']; } else if(num.matches("[1-9][1-9][0][1-9]")){ numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + "零"+digit[ch[3]-'0']; } else if(num.matches("[1-9][1-9][1-9][0]")){ numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"; } break; default: numStr = numStr + digit[ch[0]-'0'] + "仟" +digit[ch[1]-'0'] + "百" + digit[ch[2]-'0']+"拾"+digit[ch[3]-'0']; } // System.out.println(numStr); return numStr; } }
相关文章推荐
- Java学习之对象实例化
- 【读书笔记】20150905Java编程那些事儿
- basicjava
- SpringMVC基础-模型和视图ModelAndView
- java整型byte,short,int,long取值范围大小
- java_自定义异常、打包、自定义模块模式、
- Java collections小知识收集
- playframework简单入门
- java实习--json格式串记录
- Java Filter(拦截器)
- HashMap源码分析
- JAVA学习——多线程
- Java类中各成员及函数方法的执行顺序
- 用Java语言编写一个金字塔程序
- Java中隔离容器的实现
- JAVA注解
- Java设计模式-1-概述
- Java 解析chm文件实战(原创)
- Eclipse集成javah
- A a1 = new A(); A a2 = new B(); B b = new B(); C c = new C();