zoukankan      html  css  js  c++  java
  • 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);
    
            }
    
        }
    }
  • 相关阅读:
    网友谈:Dictionary.ContainsKey和List.BinarySearch哪个效率高
    C# WinForm 中在窗口标题栏上加按钮
    将Txt文件转换成dataset[原创]
    四个常见的排序算法[原创]
    改版后的groupbox[原创]
    转 五种提高 SQL 性能的方法
    转 牢记!SQL Server数据库开发的二十一条军规(SQL收藏)
    源码详解Java的反射机制
    java多线程采集+线程同步
    jQgrid API
  • 原文地址:https://www.cnblogs.com/nb08611033/p/8926756.html
Copyright © 2011-2022 走看看