C# VS2017 winForm 金额小写转大写小程序

 

1.界面

 

2.窗体代码

using System;
using System.Windows.Forms;

namespace RMBMoneyToUpper
{
    public partial class Test_Form_NumToStr : Form
    {
        public Test_Form_NumToStr()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string num = textBox1.Text.Trim();
       
MoneyToUpper moneyToUpper = ();
        textBox2.Text = moneyToUpper(num); } } }

 

3.MoneyToUpper类代码(写在MoneyToUpper.cs文件内)

using System;
using System.Text.RegularExpressions;

namespace htwj_order_print
{
    class MoneyToUpper
    {
        internal string GetMoneyUpper(string num)
        {//金额小写转大写

            string numStr = "不要输入奇怪的内容,仅支持0.01~999999999.99范围内的数字";//存储转换结果
            
            if (RegexAmount(num))
            {//输入的金额合法

                string[] upper = { "", "", "", "", "", "", "", "", "", "", "" };//数字大写
                string[] str0 = num.Split('.');//分割整数部分,小数部分
                string str1 = GetIntUpper(upper, str0[0]);//存储整数部分
                string str2 = "";//存储小数部分

                
                if (str1 != "")
                {

                    if (str0.Length == 1)
                    {//没有小数

                        if (str0[0] == "0")
                        {//只输入了一个0
                            str1 = "零圆";
                        }

                    }

                    else if (str0.Length == 2 && str0[1].Length > 0)
                    {//有小数,转换小数
                        str2 = GetDecUpper(upper, str0[1]);
                    }

                    numStr = (str1 + str2);//合并整数和小数
                }
            }

            return numStr;
        }


        private string GetIntUpper(string[] upper, string str)
        {//处理整数部分

            string intUpper = "";//存储转换后的整数大写
            string[] unit = { "", "", "", "", "", "", "", "", "亿" };//单位数组
            string strUnit = "";//存储单位
            string strUpper = "";//存储大写
            int index = 0;//存储单位数组下标
            int strLen = str.Length;//存储数字个数

            if (strLen < 10)
            {//限制整数部分长度,不能超过亿

                if (str[0].ToString() == "0")
                {//整数部分第一个数为0

                    if (str.Length == 1)
                    {//整数部分有且仅有一个0(0.1之类)
                        intUpper = "零圆";
                    }

                }

                else
                {

                    for (int i = 0; i < strLen; i++)
                    {//遍历整数部分字符串

                        //通过下标,将小写转大写
                        index = Convert.ToInt32(str[i].ToString());
                        strUpper = upper[index].ToString();

                        strUnit = unit[strLen - i - 1].ToString();//添加单位
                        intUpper += (strUpper + strUnit);//连接成字符串
                    }

                    //处理多余的零(这里实在想不出更好的办法了,感谢https://blog.csdn.net/lucky51222/article/details/26388717)
                    intUpper = intUpper.Replace("零拾", "");
                    intUpper = intUpper.Replace("零佰", "");
                    intUpper = intUpper.Replace("零仟", "");
                    intUpper = intUpper.Replace("零零零", "");
                    intUpper = intUpper.Replace("零零", "");
                    intUpper = intUpper.Replace("零零零万", "");
                    intUpper = intUpper.Replace("零零万", "");
                    intUpper = intUpper.Replace("零万", "");
                    intUpper = intUpper.Replace("零零零圆", "");
                    intUpper = intUpper.Replace("零零圆", "");
                    intUpper = intUpper.Replace("零圆", "");
                    intUpper = intUpper.Replace("亿万", "亿");

                }
            }

            return intUpper;
        }


        private string GetDecUpper(string[] upper, string str)
        {//处理小数部分

            string decUpper = "";//存储小数部分大写
            string[] unit = { "", "" };

            for (int j = 0; j < str.Length; j++)
            {//遍历小数部分数字

                //通过下标,将小写转大写
                int index = Convert.ToInt32(str[j].ToString());
                decUpper += (upper[index].ToString() + unit[j].ToString());
            }

            return decUpper;
        }


        private static bool RegexAmount(string Integer)
        {//判断输入的是否为金额(整数 || 2位小数)

            Regex g = new Regex(@"^\d{1,9}(.\d{1,2})?$");
            return g.IsMatch(Integer);

        }

    }
}

 

转载于:https://www.cnblogs.com/nb08611033/p/8926756.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值