人民币数字转化为文字, 如1235, 输出一千二百三十五元

解决方案 »

  1.   

    想不到我也能做个大概出来,哈哈刚学api查的不熟练(算法就别考虑了我怕)import java.util.*;
    public class ChineseMoney
    {
        private int s;
        ChineseMoney(int s)
        {
            this.s=s;
        }
        private String changeNum(int num)
        {
            String[] x="一二三四五六七八九十".split("");
            return x[num];
        }
        private String count(int num)
        {
            String[] x={"元","十","百","千","万","十","百","千","亿"};
            return x[num];
        }
        public void print()
        {
            String money = Integer.toString(s);
            int size = money.length();
            for(int i=0;i<size;i++)
            {
                int temp = Integer.parseInt(money.charAt(i)+"");
                String upperCase = changeNum(temp);
                String count = count(size-i-1);
                System.out.print(upperCase+count);
            }
        }
        public static void main(String[] args)
        {
            ChineseMoney money = new ChineseMoney(3244323);
            money.print();
        }
    }
      

  2.   


    import java.util.*;
    public class ChineseMoney
    {
        private int s;
        ChineseMoney(int s)
        {
            this.s=s;
        }
        private String changeNum(int num)
        {
            String[] x="零一二三四五六七八九十".split("");
            return x[num+1];
        }
        private String count(int num)
        {
            String[] x={"元","十","百","千","万","十","百","千","亿"};
            return x[num];
        }
        public void print()
        {
            String loc;
            String money = Integer.toString(s);
            int size = money.length();
            boolean flagOfZero = false; 
            for(int i=0;i<size;i++)
            {
                int temp = Integer.parseInt(money.charAt(i)+"");
                String upperCase = changeNum(temp);
                if(temp!=0)
                    loc = count(size-i-1);
                else if(!flagOfZero)
                {
                    loc = "";
                    flagOfZero = true;
                }
                else
                {
                    upperCase="";
                    loc="";
                }
                System.out.print(upperCase+loc);
            }
        }
        public static void main(String[] args)
        {
            ChineseMoney money = new ChineseMoney(7651004);
            money.print();
        }
    }
      

  3.   

    没时间具体做完,基本思路如下:public class NuToCh{
    String s[]="12345";
    String unit=null;
    for(int i=0;i<s.length;i++){
       unit=Unit.getUnit(s);//调用下面的方法取得当前位的计数单位
               //截取s的第一个字符,加上unit打印出来;此时s应该是2345
               
    }
    }class Unit{
    //下面根据长度,相应返回计数单位:s
     public static String getUnit(String s){
      int i=s.length;
      case 1: .....
      case 2: .....
      .
      .
      .
     }
    }按上面的方法应该可以了
      

  4.   

    public class Reader {
      private String strNum;
      private String strNumChFormat;
      private String strNumTemp;
      private int intNumLen;
      private String strBegin;
      public Reader(String strNum) {
        this.strNum = strNum;
      }
      public boolean check(String strNum) {
        boolean valid = false;
        
        if (strNum.substring(0,1).equals("0")){
         this.strNum = strNum.substring(1);
        }
        try {
          new Double(strNum);
          valid = true;
        }
        catch (NumberFormatException ex) {
          System.out.println("Bad number format!");
        }
        return valid;
      }
      public void init() {
        strNumChFormat = "";
        intNumLen = strNum.length();
        strNumTemp = strNum;
        strNumTemp = strNumTemp.replace('1', '一');
        strNumTemp = strNumTemp.replace('2', '二');
        strNumTemp = strNumTemp.replace('3', '三');
        strNumTemp = strNumTemp.replace('4', '四');
        strNumTemp = strNumTemp.replace('5', '五');
        strNumTemp = strNumTemp.replace('6', '六');
        strNumTemp = strNumTemp.replace('7', '七');
        strNumTemp = strNumTemp.replace('8', '八');
        strNumTemp = strNumTemp.replace('9', '九');
        strNumTemp = strNumTemp.replace('0', '零');
        strNumTemp = strNumTemp.replace('.', '点');
        strBegin = strNumTemp.substring(0, 1);
      }
      public String readNum() {
        if (check(strNum)) {
          init();
          try {
            for (int i = 1, j = 1, k = 1; i < intNumLen; i++) {
              if (strNumTemp.charAt(intNumLen - 1) == '零' && i == 1) {
                strNumChFormat = "位";
              }
              else if (strNumTemp.charAt(intNumLen - i) == '零' && j == 1) {
                strNumChFormat = "位" + strNumChFormat;
              }
              else if (strNumTemp.charAt(intNumLen - i) == '点') {
                j = 1;
                k = 1;
                strNumChFormat = strNumTemp.charAt(intNumLen - i) + strNumChFormat;
                continue;
              }
              else {
                strNumChFormat = strNumTemp.charAt(intNumLen - i) + strNumChFormat;
              }
              if (strNumTemp.charAt(intNumLen - i - 1) != '位' &&
                  strNumTemp.charAt(intNumLen - i - 1) != '零') {
                if (j == 1 && i < intNumLen) {
                  strNumChFormat = '拾' + strNumChFormat;
                }
                else if (j == 2 && i < intNumLen) {
                  strNumChFormat = '百' + strNumChFormat;
                }
                else if (j == 3 && i < intNumLen) {
                  strNumChFormat = '千' + strNumChFormat;
                }
              }
              if (j == 4 && i < intNumLen) {
                j = 0;
              }
              if (k == 4 && i < intNumLen) {
                strNumChFormat = '万' + strNumChFormat;
              }
              else if (k == 8 && i < intNumLen) {
                k = 0;
                strNumChFormat = '亿' + strNumChFormat;
              }
              j++;
              k++;
            }
            while (strNumChFormat.indexOf("位") != -1) {
              strNumChFormat = strNumChFormat.replaceAll("位", " ");
            }
            if (strNumChFormat.substring(0, 2) == "一拾") {
              strNumChFormat = strNumChFormat.substring(1, strNumChFormat.length());
            }
            if (strNumChFormat.indexOf("点") >= 0) {
              String rebegin = strNumChFormat.substring(0,
                  strNumChFormat.indexOf("点"));
              String relast = strNumChFormat.substring(strNumChFormat.indexOf("点"),
                  strNumChFormat.length());
              for (int i = 1; i <= relast.length(); i++) {
                relast = relast.replaceAll("拾", "");
                relast = relast.replaceAll("百", "");
                relast = relast.replaceAll("千", "");
                relast = relast.replaceAll("万", "");
                relast = relast.replaceAll("亿", "");
              }
              strNumChFormat = rebegin + relast;
            }
          }
          catch (ArrayIndexOutOfBoundsException ex) {
            ex.printStackTrace();
          }
          catch (Exception ex) {
            ex.printStackTrace();
          }
          int off = strNumChFormat.indexOf("点");
          strNumChFormat = strBegin + strNumChFormat.substring(0);
        }
        else {
          strNumChFormat = "";
        }
        return strNumChFormat;
      }
      public static void main(String args[]) {
        try {
          String number = args[0].toString();
          System.out.println("The number is: " + number);
          Reader reader = new Reader(number);
          System.out.println("Output String: " + reader.readNum());
        }
        catch (Exception ex) {
          System.out.println("Please input like that: javac Reader <number>");
        }
      }

    我这里正好有这个题的答案,但就是看不明白,头晕中哪位大哥帮忙在里面注释一下呀
      

  5.   


    public class NumToRMB {    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String[] item = {"零","一","二","三","四","五","六","七","八","九"};
            String[] count = {"元","十","百","千","万","十","百","千","亿","","十","百",""};
            long rmb = 1000000301;
            //最后结果:一十亿零三百零一元
            //long rmb = 1206004045;
            //最后结果:一十二亿零六百万四千零四十五元
            String result = "";
            int i = 0;
             
            int length = String.valueOf((int)rmb).length();
            //获取最高位数值
            int highest = (int)(rmb / Math.pow(10,length - 1));
            while(rmb / 10 > 0){
                
                int index = (int) rmb % 10;//得到各个位数上的数值
                String tmp = "";
                if (index == 0){
                    if ((!count[i].equals("万") && !count[i].equals("亿") && !count[i].equals("元") )){
                
                        tmp = item[index];
                    }else{
                        tmp = item[index] + count[i];
                    }
                }else{
                tmp = item[index] + count[i];
                }
                rmb = rmb / 10;
                 
                result = tmp + result;
                i++;
            }
            result = item[highest] + count[length] + result;
            System.out.println(result);
            //处理零
            System.out.println(getString(result));
        }
        
        public static String getString(String s){
            //此顺序不能改变
            s = s.replaceAll("(零{4})(万|亿|元)","");
            s = s.replaceAll("(零{3})(万|亿|元)","$2");
            s = s.replaceAll("零{2}","零");
            s = s.replaceAll("(零)(万|亿|元)","$2");
            s = s.replaceAll("零{2}","零");
            return s;
        }}
      

  6.   

    上次在论坛里有个高手(忘了名字)写了一个现贴到这供大家参考:import java.text.DecimalFormat; 
    import java.util.Scanner;public class ChineseCurrency{ 
        public static void main(String[] args) {

            Scanner scan = new Scanner(System.in);
            System.out.println("please input the price:");
            double number = scan.nextDouble(); 
               
            System.out.println(toChineseCurrency(new Double(number)));     
        } 
         
        public static String toChineseCurrency(Object o) { 
            if(o instanceof Number) { 
                String s = new DecimalFormat("#.00").format(o); 
                System.out.println(s); 
                s = s.replaceAll("\\.", "");         
                char[] digit = { '零',  '壹',  '贰',  '叁',  '肆', 
                         '伍',  '陆',  '柒',  '捌',  '玖'};         
                String unit = "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分"; 
                int l = unit.length(); 
                StringBuffer sb = new StringBuffer(unit); 
                for(int i=s.length()-1; i >=0; i--)  
                    sb = sb.insert(l-s.length()+i, digit[(s.charAt(i) - 0x30)]);             
                s = sb.substring(l-s.length(), l+s.length()); 
                s = s.replaceAll("零[拾佰仟]", "零"). 
                    replaceAll("零{2,}", "零"). 
                    replaceAll("零([兆万元])", "$1"). 
                    replaceAll("零[角分]", ""); 
                return s; 
            } else { 
                throw new NumberFormatException(); 
            } 
        } 

      

  7.   

    说明,未处理零的情况
    //金额转换,阿拉伯数字转换为中文大写
    char[] c1 = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
    char[] c2 = {' ','拾','佰','仟','萬'};
    //逻辑,数组num的下标+数组c2的下标=num.length-1;
    int[] num = new int[10];
    int index1 = num.length-1;
    //拆分数字
    int k = 1235;
    while(k != 0){
    //拆分出的数字
    num[index1] = k % 10;
    index1--;
    //去掉个位
    k = k / 10;
    }
    for(int i = index1 + 1;i < num.length;i++){
    //数字转换
    System.out.print(c1[num[i]]);
    //输出位权
    System.out.print(c2[num.length - 1 - i]);
    }
      

  8.   

    import   java.text.DecimalFormat;   
    import   java.util.Scanner; 
    public   class   ChineseCurrency{   
            public   static   void   main(String[]   args)   {                 Scanner   scan   =   new   Scanner(System.in); 
                    System.out.println("please   input   the   price:"); 
                    double   number   =   scan.nextDouble();   
                          
                    System.out.println(toChineseCurrency(new   Double(number)));           
            }   
              
            public   static   String   toChineseCurrency(Object   o)   {   
                    if(o   instanceof   Number)   {   
                            String   s   =   new   DecimalFormat("#.00").format(o);   
                            System.out.println(s);   
                            s   =   s.replaceAll("\\.",   "");                   
                            char[]   digit   =   {   '零',     '壹',     '贰',     '叁',     '肆',   
                                              '伍',     '陆',     '柒',     '捌',     '玖'};                   
                            String   unit   =   "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分";   
                            int   l   =   unit.length();   
                            StringBuffer   sb   =   new   StringBuffer(unit);   
                            for(int   i=s.length()-1;   i   > =0;   i--)     
                                    sb   =   sb.insert(l-s.length()+i,   digit[(s.charAt(i)   -   0x30)]);                           
                            s   =   sb.substring(l-s.length(),   l+s.length());   
                            s   =   s.replaceAll("零[拾佰仟]",   "零").   
                                    replaceAll("零{2,}",   "零").   
                                    replaceAll("零([兆万元])",   "$1").   
                                    replaceAll("零[角分]",   "");   
                            return   s;   
                    }   else   {   
                            throw   new   NumberFormatException();   
                    }   
            }   
    }   ----------------------------------------
    的确是很强 不得不顶一下
      

  9.   

    应该要注意一些特殊的地方,比如零的处理,十元而不是十零元,十亿而不是一十亿,等情况,而且要注意不要限定金额长度,像15楼的"仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分"不可取,还应注意运行的效率.replaceAll方法用多了,性能会慢
    public class NumberToChinese {
    public static void main(String[] args) {
    NumberToChinese t = new NumberToChinese();
     long t1 = System.currentTimeMillis();
    String[] num = new String[5];
    num[0] = "1234567890";
    num[1] = "1000000000";
    num[2] = "1000500022";
    num[3] = "1000000000000000000";
    num[4] = "10000000000000";
    for(int j =0;j<10000;j++){
    for (int i = 0; i < num.length; i++) {
    t.conversion(num[i]);
    }
    }
    long t2 = System.currentTimeMillis() - t1 ;
    System.out.println("运算时间:"+t2);
    } public void conversion(String money){
    String[] cn = {"零","一","二","三","四","五","六","七","八","九"};
    String[] unit = {"十","百","千"};
    StringBuffer cnMoney = new StringBuffer();
    int m ;
    int b = 0;
    int le = money.length();
    for(int i = 0;i<le;i++){
    m = Integer.parseInt( money.substring(i,i+1) );
    b += m ;
    if( !(m == 0 && cnMoney.charAt(cnMoney.length()-1) == '零') ){
    cnMoney.append(cn[m]);
    }
    int j = (money.length()-i-1) % 4 ;
    if( j == 0 ){
    if (cnMoney.charAt(cnMoney.length() - 1) == '零') {
    cnMoney.deleteCharAt(cnMoney.length() - 1);
    }
    if( (money.length()-i-1)/4%2==0){
    cnMoney.append("亿");
    }else{
    if(b != 0){
    cnMoney.append("万");
    }
    }
    b = 0 ;
    }else{
    if(m != 0){
    cnMoney.append(unit[j-1]);
    }
    }
    }
    if(cnMoney.charAt(0)=='一' && cnMoney.charAt(1)=='十'){
    cnMoney.deleteCharAt(0);
    }
    cnMoney.replace(cnMoney.length()-1, cnMoney.length(), "元");
    System.out.println(money+"    "+cnMoney);
    }
    }
      

  10.   

    public class numberToChineseTest {
        
        /** Creates a new instance of numberToChineseTest */
        public numberToChineseTest() {
        }
        @Test(groups = {"fast"})
        public void numberToChinese(){
            String number="6546378974";
            StringBuffer strnum=new StringBuffer();
            String number2=null;
            Map <Integer,String> nummap=new HashMap();
            Map <String ,String>stringmap=new HashMap();
            nummap.put(1,"十");
            nummap.put(2,"百");
            nummap.put(3,"千");
            nummap.put(4,"万");
            nummap.put(5,"十");
            nummap.put(6,"百");
            nummap.put(7,"千");
            nummap.put(8,"亿");
            nummap.put(9,"十");
            nummap.put(10,"百");
            nummap.put(11,"千");
            stringmap.put("1","一");
            stringmap.put("2","二");
            stringmap.put("3","三");
            stringmap.put("4","四");
            stringmap.put("5","五");
            stringmap.put("6","六");
            stringmap.put("8","八");
            stringmap.put("9","九");
            stringmap.put("0","零");
            stringmap.put("7","七");
            for (int i = 0; i < number.length(); i++) {
                number2=number2.valueOf(number.charAt(i));
                Set<String> se=stringmap.keySet();
                Iterator<String> it =se.iterator();
                while (it.hasNext()){
                    String key =it.next();
                    if(number2.equals(key)){
                        strnum.append(stringmap.get(key));
                        Integer key2=number.length()-1-i;
                        if(key2!=0){
                               strnum.append(nummap.get(key2));
                        }
                     
                        
                    }
                }
                
            }
            
            System.out.println(strnum.toString()+"元整");
            
        }
        
    }
      

  11.   

    import java.util.Stack;public class change {private int num;public change(int num){
    this.num=num;  
    }  
    public String  c1 (int num){
    String a[]={"零","一","二","三","四","五","六","七","八","九","十"};
    return a[num];
    }public String c2 (int pos){
    String b[]={"元","十","百","千","万","十","百","千","意","十","百","千"};
    return b[pos];
    }
    void modify()
       {
    Stack<String>  stack=new Stack<String>();   int len=String.valueOf(num).length();
       int m=0;
    while(len>0){
     int index = (int) num % 10;//得到各个位数上的数值
         num=num/10;
          
         stack.push(c2(m));
        
         stack.push(c1(index));
         len--;
         m++; 
    }  

     String outp="";
     while(!stack.empty()){

      outp+=stack.pop();
     }
     String out1=outp.replaceAll("零+千", "零");
     String out2=out1.replaceAll("零+百", "零");
     String out3=out2.replaceAll("零+意", "意");
     String out4=out3.replaceAll("零+十", "零");
     String out5=out4.replaceAll("零+万", "万");
     String out6=out5.replaceAll("零元", "元");
     String out7=out6.replaceAll("意万", "意");
     String out8=out7.replaceAll("^一十", "十");
     System.out.print(out8);
    } public static void main(String[] args) { change   reader =  new   change(564871003); 
    reader.modify();
    }}
      

  12.   

    这个题到处都有代码  用map还是不错的
      

  13.   

    import java.text.DecimalFormat;
    import java.util.Scanner;public class ChineseCurrency {
    public static void main(String[] args) { Scanner scan = new Scanner(System.in);
    System.out.println("please   input   the   price:");
    double number = scan.nextDouble(); System.out.println(toChineseCurrency(new Double(number)));
    } public   static   String   toChineseCurrency(Object   o)   {   
                    if(o   instanceof   Number)   {   
                            String   s   =   new   DecimalFormat("#.00").format(o);   
                            System.out.println(s);   
                            s   =   s.replaceAll("\\.",   "");                   
                            char[]   digit   =   {   '零',     '壹',     '贰',     '叁',     '肆',   
                                              '伍',     '陆',     '柒',     '捌',     '玖'};                   
                            String   unit   =   "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分";   
                            int   l   =   unit.length();   
                            StringBuffer   sb   =   new   StringBuffer(unit);   
                            for(int   i=s.length()-1;   i   >=0;   i--)     
                                    sb   =   sb.insert(l-s.length()+i,   digit[(s.charAt(i)   -   0x30)]);                           
                            s   =   sb.substring(l-s.length(),   l+s.length());   
                            s   =   s.replaceAll("零[拾佰仟]",   "零").   
                                    replaceAll("零{2,}",   "零").   
                                    replaceAll("零([兆万元])",   "$1").   
                                    replaceAll("零[角分]",   "");   
                            return   s;   
                    }   else   {   
                            throw   new   NumberFormatException();   
                    }   
            }}
      

  14.   

    public class NumChange
    {
    public static void main(String args[])
    {
    String theStr_0="";
    int theNum_0 = 0;
    String theStr_1="";
    theStr_0 = args[0];
    int j=theStr_0.length()-1;
    String arr[] = null;
    for(int i=0;i<theStr_0.length();i++)
    {
    theNum_0 = Integer.parseInt(theStr_0.substring(i,i+1));
    switch(theNum_0)
    {
    case 1:theStr_1+="一";break;
    case 2:theStr_1+="二";break;
    case 3:theStr_1+="三";break;
    case 4:theStr_1+="四";break;
    case 5:theStr_1+="五";break;
    case 6:theStr_1+="六";break;
    case 7:theStr_1+="七";break;
    case 8:theStr_1+="八";break;
    case 9:theStr_1+="九";break;
    case 0:theStr_1+="零";break;
    }
    if(theNum_0!=0)
    {
    switch(j)
    {
    case 1:theStr_1+="十";break;
    case 2:theStr_1+="百";break;
    case 3:theStr_1+="千";break;
    case 5:theStr_1+="十";break;
    case 6:theStr_1+="百";break;
    case 7:theStr_1+="千";break;
    case 9:theStr_1+="十";break;
    case 10:theStr_1+="百";break;
    case 11:theStr_1+="千";break;
    }
    }
    if(j==4)
    {
    theStr_1+="万";
    }
    if(j==8)
    {
    theStr_1+="亿";
    }
    j--;
    }
    theStr_1 = theStr_1.replaceAll("零+", "零");
    if(theStr_1.substring(theStr_1.length()-1,theStr_1.length()).equals("零"))
    {
    theStr_1 = theStr_1.substring(0,theStr_1.length()-1);
    }
    theStr_1 = theStr_1.replaceAll("零万", "万");
    theStr_1 = theStr_1.replaceAll("零亿", "亿");
    System.out.println(theStr_1);
    }
    }
    不会玩正则表达式,差不多拉,呵呵