C#把数字转换为大写金额

 private string ToBigNumber(long number)
        {
            // 12345 一万贰仟叁佰肆拾伍
            string[] mm ={ "", "拾", "佰", "仟", "万", "拾", "佰",

                          "仟", "亿", "拾", "佰", "仟", "万" };
            string[] dx = { "零", "壹", "贰", "叁", "肆", "伍",

                      "陆", "柒", "捌", "玖", "拾" };

            if (number == 0)
            {
                return dx[0];
            }
            string numberStr = number.ToString();
            if (numberStr.Length > mm.Length)
            {
                throw new UserException("can not parser number, as it it too long.");
            }
            StringBuilder buff = new StringBuilder();
            int flag = 0;
            int preNum = -1;
            for (int i = numberStr.Length - 1; i >= 0; i--)
            {
                string currentBit = numberStr.Substring(flag++, 1);
                if (int.Parse(currentBit) == 0)// deal with 0.
                {
                    if (preNum != 0)// deal with 000
                    {
                        buff.Append(dx[int.Parse(currentBit)]);
                    }
                }
                else
                {
                    buff.Append(dx[int.Parse(currentBit)]).Append(mm[i]);
                }
                preNum = int.Parse(currentBit);
            }

            //remove the latest zero.
            string result = buff.ToString();
            if (result.EndsWith(dx[0]))
            {
                buff.Remove(buff.Length - 1, 1);
            }
            return buff.ToString();

        }

posted @ 2009-12-10 22:29  tangself  阅读(429)  评论(0编辑  收藏  举报