数字金额转换大写人民币

[亡魂溺海] 提交于 2020-01-08 23:34:10

 

using System;
using System.Collections.Generic;
using System.Text;

namespace PublicClass
{
    /// <summary>
    /// Copyright(c) 2008~2011 KingBoy Software Co.,Ltd
    /// FileName:大写人民币类
    /// Author:KingBoy  Version:1.0.0.0  Date:2009-3-05
    /// Description:提供将数值型输入转变成人民币大写的标准类。 
    /// </summary>
    public class ChineseMoney
    {
        private decimal dMoney = 0; //金额
        /// <summary>
        /// 设置或获取金额
        /// </summary>
        public decimal money
        {
            get { return dMoney; }
            set { dMoney = value; }
        }
        public ChineseMoney(decimal m)
        {
            dMoney = m;
        }
        /// <summary>
        /// 获取金额大写
        /// </summary>
        /// <returns></returns>
        public string getChineseMoney()
        {
            try
            {
                string strcmd = Convert.ToString(dMoney); //将金额转换为字符串型
                string[] temp = strcmd.Split('.');
                string[] integer ={"元","拾","佰","仟","万","拾万","佰万","仟万","亿","拾亿","佰亿","仟亿","万亿","亿亿" };
                string[] infrex ={"角","分" };
                string[] data ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
                string result = "";
                if (temp.Length > 0)
                {
                    /*
                     处理整数部分
                     */
                    string ch = "";
                    for (int i = 0; i < temp[0].Length; i++)
                    {
                        switch (Convert.ToInt32(temp[0].Substring(i, 1)))
                        {
                            case 0:
                                ch = data[0]; break;
                            case 1:
                                ch = data[1]; break;
                            case 2:
                                ch = data[2]; break;
                            case 3:
                                ch = data[3]; break;
                            case 4:
                                ch = data[4]; break;
                            case 5:
                                ch = data[5]; break;
                            case 6:
                                ch = data[6]; break;
                            case 7:
                                ch = data[7]; break;
                            case 8:
                                ch = data[8]; break;
                            case 9:
                                ch = data[9]; break;
                            default:
                                ch = ""; break;
                        }
                        if (Convert.ToInt32(temp[0].Substring(i, 1)) > 0)
                            result =  result+ ch +integer[temp[0].Length-i-1];
                        else
                            result = result +ch ;
                    }
                }
                  if (temp.Length > 1) //处理小数部分
                  { 
                     string ch = "";
                  for (int i = 0; i < temp[1].Length; i++)
                  {
                      switch (Convert.ToInt32(temp[1].Substring(i, 1)))
                      {
                          case 0:
                              ch = data[0]; break;
                          case 1:
                              ch = data[1]; break;
                          case 2:
                              ch = data[2]; break;
                          case 3:
                              ch = data[3]; break;
                          case 4:
                              ch = data[4]; break;
                          case 5:
                              ch = data[5]; break;
                          case 6:
                              ch = data[6]; break;
                          case 7:
                              ch = data[7]; break;
                          case 8:
                              ch = data[8]; break;
                          case 9:
                              ch = data[9]; break;
                          default:
                              ch = ""; break;
                      }
                      if (Convert.ToInt32(temp[1].Substring(i, 1)) > 0)
                          result =result+ch+infrex[i] ;
                      else
                          result = result +ch;
                  }

                }
                return result;

            }
            catch (Exception)
            {
                return "转换失败!";
            }
        }
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!