C# Product amount case conversion

Specific requirements: Enter the amount in lowercase in the text box, click the conversion button, and the amount representation in uppercase will be automatically generated.

The result is shown in the figure

code show as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 定义一个转换函数
        private static string NumeriToChinese(int amount)
        {
            string[] chineseNumbers = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            string[] units = { "", "拾", "佰", "仟", "万", "十万", "百万", "千万", "亿" };

            string str = amount.ToString(); // 将输入的数字转为字符串
            int n = str.Length; // 获取字符串长度
            string res = ""; // 定一个空字符串接收输出金额

            for (int i = 0;i < n; i++)
            {
                int num = str[i] - '0';
                if (num != 0)
                {
                    res += chineseNumbers[num];
                    res += units[n - 1 - i];

                }
                else if (i != n - 1 && str[i+1] != '0')
                    res += chineseNumbers[num];
            }
            return res;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            int p;
            if (int.TryParse(textBox1.Text,out p))
            {
                textBox2.Text = NumeriToChinese(p);
            }
        }

    
    }
}

Guess you like

Origin blog.csdn.net/qq_62238325/article/details/133914934