前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#笔记:数字转大写金额

C#笔记:数字转大写金额

作者头像
超级大猪
发布2019-11-22 09:45:38
1.5K0
发布2019-11-22 09:45:38
举报

好像没有什么问题了耶。 调用 :

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.textBox1.TextChanged -= new EventHandler(textBox1_TextChanged);
            this.textBox1.Text = GetChineseMoney.GetEngFormatNum(this.textBox1.Text);
            this.textBox1.Select(this.textBox1.TextLength, 0); 
            this.textBox2.Text = GetChineseMoney.ConvertToChineseMoney (this.textBox1.Text);
            this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
        }

类:

public class GetChineseMoney
    {
        private static string ConvertSingleUpperNum(char inputChar)
        {
            switch (inputChar)
            {
                case '1':
                    return "壹";
                case '2':
                    return "贰";
                case '3':
                    return "叁";
                case '4':
                    return "肆";
                case '5':
                    return "伍";
                case '6':
                    return "陆";
                case '7':
                    return "柒";
                case '8':
                    return "捌";
                case '9':
                    return "玖";
                case '0':
                    return "零";
            }
            return "";
        }
        /// <summary>
        /// 把4位数字转换成相应的形式
        /// </summary>
        /// <param name="inputStr">传入的字符</param>
        /// <returns>处理好的大写字符</returns>
        private static string Convert4Num(string inputStr)
        {
            //while(inputStr.Length<4)
            //{
            //    inputStr = "0" + inputStr;
            //}
            string result = "";
            result += ConvertSingleUpperNum(inputStr[0]) + "千";
            result += ConvertSingleUpperNum(inputStr[1]) + "百";
            result += ConvertSingleUpperNum(inputStr[2]) + "十";
            result += ConvertSingleUpperNum(inputStr[3]);
            result = result.Replace("零千", "零");
            result = result.Replace("零百", "零");
            result = result.Replace("零十", "零");
            return result;
        }
        //每四位获取单位
        private static string[] GetUnit(int length)
        {
            string[] result = new string[length];
            result[length - 1] = "";
            for (int i = length - 2; i >= 0; i--)
            {
                if ((i - length) * (-1) % 2 == 0)
                {
                    result[i] = "万";
                }
                else
                {
                    result[i] = "亿";
                }
            }
            return result;
        }
        //主方法
        public static string ConvertToChineseMoney(string inputStr)
        {
            string[] strArray = null;
            if (inputStr.IndexOf('.') >= 0)
            {
                strArray = inputStr.Split('.');
                if (strArray == null || strArray.Length != 2)
                {
                    return "错误";
                }
                string strAbovePoint = ConvertInt(strArray[0]);
                string strAfterPoint = "";
                if (strArray[1].Length == 1)
                {
                    strAfterPoint += ConvertSingleUpperNum(strArray[1][0]) + "角";
                }
                if (strArray[1].Length >= 2)
                {
                    strAfterPoint += ConvertSingleUpperNum(strArray[1][0]) + "角" + ConvertSingleUpperNum(strArray[1][1]) + "分";
                }
                return strAbovePoint + "元" + strAfterPoint;
            }
            else
            {
                return ConvertInt(inputStr);
            }
        }
        private static string ConvertInt(string inputStr)
        {
            if (inputStr.Length >= 22)
            {
                return "溢出";
            }
            if (string.IsNullOrEmpty(inputStr))
            {
                return "";
            }
            string result = "";
            inputStr = inputStr.Replace(",", "");
            while (inputStr.Length % 4 != 0)
            {
                inputStr = "0" + inputStr;
            }
            int colLength = inputStr.Length / 4;
            string[] strCol = new string[colLength];
            for (int i = 0, j = 0; i < inputStr.Length; i = i + 4, j++)
            {
                strCol[j] = inputStr.Substring(i, 4);
            }
            string[] danwei = GetUnit(colLength);
            for (int i = 0; i < colLength; i++)
            {
                if (strCol[i] == "0000" && danwei[i] == "万")
                {
                    result += Convert4Num(strCol[i]);
                }
                else
                {
                    result += Convert4Num(strCol[i]) + danwei[i];
                }
            }
            while (result.IndexOf("零零") >= 0)
            {
                result = result.Replace("零零", "零");
            }
            result = result.Replace("零万", "万");
            result = result.Replace("零亿", "亿");
            while (result.StartsWith("零"))
            {
                result = result.Substring(1);
            }
            while (result.EndsWith("零"))
            {
                result = result.Substring(0, result.Length - 1);
            }
            return result;
        }
        
        public static string GetEngFormatNum(string inputStr)
        {            
            string tempStr = inputStr.Replace(",", "");            
            string tail = "";
            if (tempStr.IndexOf('.') >= 0)
            {
                tail = tempStr.Substring(tempStr.IndexOf('.'));                
                tempStr = tempStr.Substring(0, tempStr.IndexOf('.'));
            }
            string result = "";
            for (int i = tempStr.Length - 1; i >= 0; i--)
            {
                if ((tempStr.Length - i) % 3 == 0)
                {
                    result = "," + tempStr[i] + result;
                }
                else
                {
                    result = tempStr[i] + result;
                }
            }
            if (result.StartsWith(","))
            {
                result = result.Substring(1);
            }
            return result + tail;
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-02-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档