A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© duanhuilin 中级黑马   /  2012-12-28 18:22  /  2211 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

看了好多关于字符串的处理方式,刚刚找到一个自己认为比较棘手的字符串处理的问题,在此希望能得到大家的帮助:
将101000001010这个金额转化成壹仟零壹拾亿零壹仟零壹拾园整
我先将我自己的代码贴一下,大家还有没有更好的思路?


public static  string NumberCn(double ANumber)
        {
            const string cPointCn = "点十百千万十百千亿十百千";
            const string cNumberCn = "零一二三四五六七八九";
            string S = ANumber.ToString();
            if (S == "0") return "" + cPointCn[0];
            if (!S.Contains(".")) S += ".";
            int P = S.IndexOf(".");
            string Result = "";
            for (int i = 0; i < S.Length; i++)
            {
                if (P == i)
                {
                    Result = Result.Replace("零十零", "零");
                    Result = Result.Replace("零百零", "零");
                    Result = Result.Replace("零千零", "零");
                    Result = Result.Replace("零十", "零");
                    Result = Result.Replace("零百", "零");
                    Result = Result.Replace("零千", "零");
                    Result = Result.Replace("零万", "万");
                    Result = Result.Replace("零亿", "亿");
                    Result = Result.Replace("亿万", "亿");
                    Result = Result.Replace("零点", "点");
                }
                else
                {
                    if (P > i)
                        Result += "" + cNumberCn[S[i] - '0'] + cPointCn[P - i - 1];
                    else Result += "" + cNumberCn[S[i] - '0'];
                }
            }
            if (Result.Substring(Result.Length - 1, 1) == "" + cPointCn[0])
                Result = Result.Remove(Result.Length - 1); // 一点-> 一
            if (Result[0] == cPointCn[0])
                Result = cNumberCn[0] + Result; // 点三-> 零点三
            if ((Result.Length > 1) && (Result[1] == cPointCn[1]) &&
               (Result[0] == cNumberCn[1]))
                Result = Result.Remove(0, 1); // 一十三-> 十三
            return Result;
        }
        string MoneyCn(double ANumber)
        {
            if (ANumber == 0) return "零";
            string Result = NumberCn(Math.Truncate(ANumber * 100) / 100);
            Result = Result.Replace("一", "壹");
            Result = Result.Replace("二", "贰");
            Result = Result.Replace("三", "叁");
            Result = Result.Replace("四", "肆");
            Result = Result.Replace("五", "伍");
            Result = Result.Replace("六", "陆");
            Result = Result.Replace("七", "柒");
            Result = Result.Replace("八", "捌");
            Result = Result.Replace("九", "玖");
            Result = Result.Replace("九", "玖");
            Result = Result.Replace("十", "拾");
            Result = Result.Replace("百", "佰");
            Result = Result.Replace("千", "仟");
            if (Result.Contains("点"))
            {
                int P = Result.IndexOf("点");
                Result = Result.Insert(P + 3, "分");
                Result = Result.Insert(P + 2, "角");
                Result = Result.Replace("点", "圆");
                Result = Result.Replace("角分", "角");
                Result = Result.Replace("零分", "");
                Result = Result.Replace("零角", "");
                Result = Result.Replace("分角", "");
                if (Result.Substring(0, 2) == "零圆")
                    Result = Result.Replace("零圆", "");
            }
            else Result += "圆整";
            Result = "人民币" + Result;
            return Result;
        }

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

3 个回复

倒序浏览
我这有一个上次的基础测试题目,是将c#时间格式转换汉字大写,比如: “2011年6月4日” 转换成 “二零一一年六月四日”。解决方法上应该是一样的,我使用switch语句写的,也不是啥号方法,你可以参考一下!!
  1. static void Main(string[] args)
  2. {
  3. string date = "2011年6月4日";//定义日期为2011年6月
  4. int num = 0;//记录循环次数
  5. char[] newdate = date.ToCharArray();//把字符串变量date转换成char类型的数组并赋给newdate
  6. for (num = 0; num < newdate.Length; num++)
  7. {
  8. switch (newdate[num])//判断第num个字符数组是什么
  9. {
  10. //如果是0,则把0替换为零,下面依次替换
  11. case '0':
  12. newdate[num] = '零';
  13. break;
  14. case '1':
  15. newdate[num] = '一';
  16. break;
  17. case '2':
  18. newdate[num] = '二';
  19. break;
  20. case '3':
  21. newdate[num] = '三';
  22. break;
  23. case '4':
  24. newdate[num] = '四';
  25. break;
  26. case '5':
  27. newdate[num] = '五';
  28. break;
  29. case '6':
  30. newdate[num] = '六';
  31. break;
  32. case '7':
  33. newdate[num] = '七';
  34. break;
  35. case '8':
  36. newdate[num] = '八';
  37. break;
  38. case '9':
  39. newdate[num] = '九';
  40. break;
  41. }
  42. }
  43. Console.WriteLine(newdate);//输出日期
  44. Console.ReadKey();

  45. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
王晨 发表于 2012-12-28 18:40
我这有一个上次的基础测试题目,是将c#时间格式转换汉字大写,比如: “2011年6月4日” 转换成 “二零一一 ...

呵呵,谢谢。不过还是有些区别的。上面那个处理字符串考虑的地方多些
回复 使用道具 举报
我的,已经错了,把错误的晒出来大家也看下:
  1. package com.itheima;

  2. /**
  3. * 金额转换,阿拉伯数字转换成中国传统形式。 例如:101000001010 转换为 壹仟零壹拾亿零壹仟零壹拾圆整
  4. *
  5. * @author Mr liu
  6. *
  7. */
  8. public class Test10 {
  9.         public static void main(String[] args) {
  10.                 System.out.println(getString(101000001010l));
  11.         }

  12.         // 定义字符串数组分别存储中国传统货币的写法
  13.         public static String[] cn = { "仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟",
  14.                         "佰", "拾", "圆整" };
  15.         public static String[] number = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒",
  16.                         "捌", "玖" };

  17.         // 定义一个获取数字转换成中国货币的方法
  18.         public static String getString(long n) {
  19.                 String money = "" + n;
  20.                 if (money.length() > cn.length)
  21.                         return "数字太长了";
  22.                 String str = "";
  23.                 for (int x = 0; x < money.length(); x++)
  24.                         str += number[Integer.parseInt("" + money.charAt(x))]
  25.                                         + cn[cn.length - money.length() + x];

  26.                 return str;
  27.         }
  28. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马