微信小程序> CVS2017 winForm金额小写转大写小程序

CVS2017 winForm金额小写转大写小程序

浏览量:2611 时间: 来源:weixin_30314813

 

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);        }    }}

 

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎