将整型数字转化成大写格式

将整型数字转化成大写格式

将整型数字转化成大写格式
我们日常报表的打印,单据打印,借据、收据打印等等操作都需要用到将金额装换成大写,防止交易双方通过修改单据数字,因而我们的信息管理系统就把账单的金额转换为大写。
下面是C#的一个将数字转换为大写的实例,解释都在代码中了,具体实现思路就是通过获取页面的整形数字,然后在后台定义大写数字数组,大写单位数组,大写位数数组;通过判断窗体传过来的值通过条件语句和循环语句循环遍历数字对应的大写数字和位数以及单位;看代码和解释如下

1、 窗体以及窗体后台代码,通过按钮点击将数字转化为大写

在这里插入图片描述

     //转换按钮点击事件
        private void btn_transform_Click(object sender, EventArgs e)
        {
            int P_int_temp;//定义整型变量
            if (int.TryParse(txt_lower.Text,out P_int_temp))
            {
                txt_upper.Text = //获取转换为大写金额的字符串
                    new Upper().NumToChinese(txt_lower.Text);
            }
            else
            {
                MessageBox.Show(//错误提示信息
                    "请输入正确整数数值","提示!");
            }
        }

2、 小写转大写封类代码

class Upper
    {
        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 = ""; //返回值
            int finger = 0; //字符位置指针
            int P_int_m = x.Length % 4; //取余
            int P_int_k = 0;
            if (P_int_m > 0) //判断余数是否大于0,大于0商加1
                P_int_k = x.Length / 4 + 1;
            else            //等于0则取整商
                P_int_k = x.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 = x.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];
                }
            }
            return P_str_returnValue;
        }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值