本帖最后由 Tking 于 2014-4-20 17:38 编辑
程序的目的:将一串数字金额,转换成汉字金额;目前是能保留两位小
数
- import java.util.ArrayList;
- import java.util.List;
- public class Test11 {
- static final String[] REGHT_DANWEI={"分","角"};
- static final String[] LEFT_DANWEI={"元","拾","佰","仟","万","拾万","佰万","仟万","亿","拾亿","佰亿","万亿","拾万亿","佰万亿","仟万亿"};
- static final String[] NUMBLES={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
- public static void main(String[] args)
- {
-
- //先判断数字是否可以转换
- String temp="00001.254";
- String str=getString(temp);
- System.out.println(str);
-
- }
- public static String getString(String str)
- {
- if(str.indexOf('.')!=-1)
- {
- String[] temp=str.split("\\.");
- return get(temp[0])+getReght(temp[1]);
- }
- return get(str)+"整";
- }
- // 判断字符串的长度与是否都是纯数字
- private static String get(String str)
- {
- if((str.length()<=LEFT_DANWEI.length)&&(str.matches("\\d+")))
- return getLeft(str);
- return "格式错误";
- }
-
- public static String getReght(String reght)
- {
- List<String> list=null;
- if(reght.length()>2)
- list=getList(reght.substring(0, 2), REGHT_DANWEI);
- else list=getList(reght, REGHT_DANWEI);
-
- if(list.size()==4&&list.get(0).equals(NUMBLES[0])&&list.get(list.size()-2).equals(NUMBLES[0]))
- list.clear();
- if(list.size()==2&&list.get(0).equals(NUMBLES[0]))
- list.clear();
- if(list.size()==4&&list.get(0).equals(NUMBLES[0]))
- list.remove(1);
- if(list.size()>=4&&list.get(list.size()-2).equals(NUMBLES[0]))
- {
- list.remove(list.size()-1);
- list.remove(list.size()-1);
- }
- if(list.size()!=0)
- return gettoString(list);
- return "整";
-
- }
-
- public static List<String> getList(String left,String[] danwei)
- {
- List<String> list=new ArrayList<String>();
- for(int i=0;i<left.length();i++)
- {
- list.add(NUMBLES[Integer.valueOf(left.charAt(i)+"")]);
- list.add(danwei[left.length()-1-i]);
- }
- return list;
- }
- // 对应查表将字符串数字转换成中文
- public static String getLeft(String left)
- {
- List<String> list=getList(left,LEFT_DANWEI);
-
- //去零操作
- delZore(list,LEFT_DANWEI[4],LEFT_DANWEI[8]);
-
- //对万位与亿单位进行检查
- delmod(list,4,4,LEFT_DANWEI);
- delmod(list,8,LEFT_DANWEI.length-8,LEFT_DANWEI);
-
- //对个位判断,如果失灵
- if(list.size()>=2&&list.get(list.size()-2).equals(NUMBLES[0]))
- list.remove(list.size()-2);
-
- // System.out.println("最终结果 \t--"+list);
-
- return gettoString(list);
- }
-
- //对零进行操作
- public static void delZore(List<String> list,String temp1,String temp2)
- {
- //去除开头位置多余零
- for(int i=0;list.get(i).equals(NUMBLES[0]);)
- {
- list.remove(i);list.remove(i);
- }
-
- //去除连续的零,但保留万位的零与亿位的零
- for(int i=0;i<list.size()-2;)
- {
- if(list.get(i).equals(NUMBLES[0])&&!list.get(i+1).equals(temp1)&&!list.get(i+1).equals(temp2))
- if(list.get(i+2).equals(NUMBLES[0]))
- {
- list.remove(i);list.remove(i);
- continue;
- }
- i++;
- }
-
- // System.out.println("去连零结果 \t--"+list);
-
- //去除多余的零单位
- for(int i=0;i<list.size()-2;i++)
- if(list.get(i).equals(NUMBLES[0]))
- {
- if(list.get(i+1).equals(temp1)||list.get(i+1).equals(temp2))
- list.remove(i);
- else list.remove(i+1);
- }
-
- // System.out.println("去零单位结果\t--"+list);
- }
-
- //去除多余单位
- public static void delmod(List<String> list,int index,int connt,String[] danwei)
- {
-
- for(int i=index;i<(connt+index);i++)
- if(list.contains(danwei[i]))
- for(int j=index+1;j<(connt+index);j++)
- if(list.contains(danwei[j]))
- list.set(list.indexOf(danwei[j]), danwei[j].substring(0, danwei[j].length()-1));
- // System.out.println("去单位结果\t--"+list);
- }
-
- //将集合类的元素组合成字符串
- public static String gettoString(List<String> list)
- {
- StringBuilder sb=new StringBuilder();
- for(String i:list)
- sb.append(i);
- return sb.toString();
- }
-
-
- }
复制代码
|