金额大小写转换

简单说金额大小写转换就是输入一串数字,将其转换为中文形式,首先封装一个类,方便以后自己调用,返回值肯定为string字符串类型,参数也是传递一个string字符串用来接收,废话不多说,自己上代码,代码中有其注解。

public string NumToChinese(string x)

        {

            //数字转换为中文后的数组

            string[] P_array_num = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

            //为数字位数建立一个位数组

            string[] P_array_digit = new string[] { "", "拾", "佰", "仟" };

            //为数字单位建立一个单位数组

            string[] P_array_units = new string[] { "", "万", "亿", "万亿" };

            string P_str_returnValue = string.Empty; //返回值

            int finger = 0; //字符位置指针

 

            string[] str = x.Split('.');//切割小数位;

            string newx_One = str[0]; //复制传递参数值,整数位

            string P_str_returnDeci = string.Empty;//小数部分

            if (str.Length > 1)

            {

                string newx_Two = str[1];

                P_str_returnDeci = DecimalToChenese(newx_Two);

            }

            string newx = newx_One;

            int Islose = 0; //判断正负数

            if (newx_One.StartsWith("-"))

            {

                Islose = -1;//负数

                newx = newx_One.Substring(1);

                string newx_Oneold = newx;

                for (int i = 0; i < newx_Oneold.Length; i++)

                {

                    //移除多余的0

                    if (newx_Oneold[i].ToString().Equals("0"))

                    {

                        newx = newx_Oneold.Substring(i + 1);

                    }

                    else

                    {

                        break;

                    }

                }

            }

            else

            {

                for (int i = 0; i < newx_One.Length; i++)

                {

                    //移除多余的0

                    if (newx_One[i].ToString().Equals("0"))

                    {

                       newx = newx_One.Substring(i + 1);

                    }

                    else

                    {

                        break;

                    }

                }

            }

 

            int P_int_m = newx.Length % 4; //取模

            int P_int_k = 0;

            if (P_int_m > 0)

                P_int_k = newx.Length / 4 + 1;

            else

                P_int_k = newx.Length / 4;

            //外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万,"

            for (int i = P_int_k; i > 0; i--)

            {

                int P_int_L = 4;

                if (i == P_int_k && P_int_m != 0)

                    P_int_L = P_int_m;

                //得到一组四位数

                string four = newx.Substring(finger, P_int_L);

                int P_int_l = four.Length;

                //内层循环在该组中的每一位数上循环

                for (int j = 0; j < P_int_l; j++)

                {

                    //处理组中的每一位数加上所在的位

                    int n = Convert.ToInt32(four.Substring(j, 1));

                    if (n == 0)

                    {

                        if (j < P_int_l - 1 && Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !P_str_returnValue.EndsWith(P_array_num[n]))

                            P_str_returnValue += P_array_num[n];

                    }

                    else

                    {

                        if (!(n == 1 && (P_str_returnValue.EndsWith(P_array_num[0]) | P_str_returnValue.Length == 0) && j == P_int_l - 2))

                            P_str_returnValue += P_array_num[n];

                        P_str_returnValue += P_array_digit[P_int_l - j - 1];

                    }

                }

                finger += P_int_L;

                //每组最后加上一个单位:",万,",",亿," 等

                if (i < P_int_k) //如果不是最高位的一组

                {

                    if (Convert.ToInt32(four) != 0)

                        //如果所有4位不全是0则加上单位",万,",",亿,"等

                        P_str_returnValue += P_array_units[i - 1];

                }

                else

                {

                    //处理最高位的一组,最后必须加上单位

                    P_str_returnValue += P_array_units[i - 1];

                }

            }

 

            if (Islose < 0)  return "负" + P_str_returnValue + "块" + P_str_returnDeci;

            return P_str_returnValue + "块" + P_str_returnDeci;

        }

//获取小数部分

        public string  DecimalToChenese(string deci)

        {

            if (deci.Length <= 1)

            {

                deci = deci + "00";

            }

            //截取,只保留两位小数(角,分);

            deci = deci.Substring(0, 2);

            //定义一个sting接收结果

            string DeciNum = deci;

            //数字转换为中文后的数组

            string[] P_array_num = new string[] { "", "一", "两", "三", "四", "五", "六", "七", "八", "九" };

            if (deci.Equals("00"))

            {

                DeciNum = string.Empty;

            }

            else if (deci.StartsWith("0"))

            {

                int num = Convert.ToInt32(deci.Substring(1));

                DeciNum = P_array_num[num] + "分";

            }

            else if (deci.EndsWith("0"))

            {

                int num = Convert.ToInt32(deci.Substring(0,1));

                DeciNum = P_array_num[num] + "毛";

            }

            else

            {

                int num = Convert.ToInt32(deci.Substring(0,1));

                int nums = Convert.ToInt32(deci.Substring(1));

                DeciNum = P_array_num[num] + "毛" +  P_array_num[nums] + "分";

            }

            return DeciNum;

        }

 

 

接着就是实例此类调用,将其写在按钮点击事件触发,你想要怎么调用都行,也可以写个文本改变事件来进行触发,代码如下:

private void btn_transform_Click(object sender, EventArgs e)

        {

            //定义整型变量,判断传递过来的参数是否为整形

            if (long.TryParse(txt_lower.Text, out long P_int_temp))

            {

                //获取转换为大写金额的字符串

                txt_upper.Text = new Upper().NumToChinese(txt_lower.Text);

            }

            else

            {

                //错误提示信息

                MessageBox.Show(string.Format("{0}值格式错误", P_int_temp));

 

            }

    }

演示结果:

金额大小写转换

上一篇:linux 域名解析


下一篇:python3模块初识