日期:2014-05-18  浏览次数:20756 次

新人刚自己写的人民币转换大小写,比较简短,希望各位能理解,支持的顶啊! (顶的就有分)
新人刚自己写的人民币转换大小写,比较简短,希望各位能理解,支持的顶啊! 

C# code

using System.Text;

namespace MoneytoChinese
{    
    class Test
    {
        static void Main()
        {
            for (; ; )
            {
                System.Console.Write("金额: ");
                string s = System.Console.ReadLine();
                decimal m = 450002030.05m;
                try { m = decimal.Parse(s); }
                catch { //break; 
                }
                System.Console.WriteLine("大写: "+MoneytoChinese(m));
            }
        }
        public static string MoneytoChinese(decimal money)
        {
            string ChineseMoney = "";
            string[] snum = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            string[] sdanwei = new string[] { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" };
            long lastnum;
            int i = 0;
            for (long tmpmoney = (long)(money * 100); tmpmoney >= 1; tmpmoney /= 10)
            {
                lastnum = tmpmoney % 10;
                ChineseMoney = snum[lastnum] + sdanwei[i++] + ChineseMoney;
            }
            ChineseMoney = ChineseMoney.Replace("零角", "零");
            ChineseMoney = ChineseMoney.Replace("零零分", "");
            ChineseMoney = ChineseMoney.Replace("零分", "");
            ChineseMoney = ChineseMoney.Replace("零拾", "零");
            ChineseMoney = ChineseMoney.Replace("零佰", "零");
            ChineseMoney = ChineseMoney.Replace("零仟", "零");
            ChineseMoney = ChineseMoney.Replace("零零", "零");
            ChineseMoney = ChineseMoney.Replace("零零", "零");
            ChineseMoney = ChineseMoney.Replace("零元", "元");
            ChineseMoney = ChineseMoney.Replace("零万", "万");
            ChineseMoney = ChineseMoney.Replace("零亿", "亿");
            ChineseMoney = ChineseMoney.Replace("亿万", "亿");
            return ChineseMoney;
        }
    }
}



(山之魂原创,著作权所有,转贴须经本人同意!)


------解决方案--------------------
look
------解决方案--------------------
^ō^ 板凳...
------解决方案--------------------
顶。不错。不过网上有好多了
------解决方案--------------------
不正确。
------解决方案--------------------
好象有个正则表达式,挺经典的
------解决方案--------------------
C# code
// 例如:(new Money(200)).ToString() == "贰佰元"
namespace Skyiv.Util
{
  using System.Text;
  class Test
  {
    static void Main()
    {
      for (;;)
      {
        System.Console.Write("金额: ");
        string  s = System.Console.ReadLine();
        decimal m;
        try   { m = decimal.Parse(s); }
        catch { break; }
        System.Console.WriteLine("大写: " + new Money(m));
      }
    }
  }
  // 该类重载的 ToString() 方法返回的是大写金额字符串
  class Money
  {
    public string Yuan  = "元";                        // “元”,可以改为“圆”、“卢布”之类
    public string Jiao  = "角";                        // “角”,可以改为“拾”
    public string Fen   = "分";                        // “分”,可以改为“美分”之类
    static string Digit = "零壹贰叁肆伍陆柒捌