文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

C#实现数字转换汉字的示例详解

2022-12-19 06:00

关注

实践过程

效果

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public static string CNumToCh(string x)
    {
        //数字转换为中文后的数组 
        string[] num = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        //为数字位数建立一个位数组 
        string[] digit = new string[] { "", "拾", "佰", "仟" };
        //为数字单位建立一个单位数组 
        string[] units = new string[] { "", ",万,", ",亿,", ",万亿," };
        string returnValue = ""; //返回值 
        int finger = 0; //字符位置指针 
        int m = x.Length % 4; //取模 
        int k = 0;
        if (m > 0)
            k=x.Length / 4 + 1;
        else
            k=x.Length / 4;
        //外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万," 
        for (int i = k; i > 0; i--)
        {
            int L = 4;
            if (i == k && m != 0)
                L = m;
            //得到一组四位数 
            string four = x.Substring(finger, L);
            int l = four.Length;
            //内层循环在该组中的每一位数上循环  
            for (int j = 0; j < l; j++)
            {
                //处理组中的每一位数加上所在的位 
                int n = Convert.ToInt32(four.Substring(j, 1));
                if (n == 0)
                {
                    if (j < l - 1&& Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !returnValue.EndsWith(num[n]))
                        returnValue += num[n];
                }
                else
                {
                    if (!(n == 1 && (returnValue.EndsWith(num[0]) | returnValue.Length == 0) && j == l - 2))
                        returnValue += num[n];
                    returnValue += digit[l - j - 1];
                }
            }
            finger += L;
            //每组最后加上一个单位:",万,",",亿," 等 
            if (i < k) //如果不是最高位的一组 
            {
                if (Convert.ToInt32(four) != 0)
                    //如果所有4位不全是0则加上单位",万,",",亿,"等 
                    returnValue += units[i - 1];
            }
            else
            {
                //处理最高位的一组,最后必须加上单位 
                returnValue += units[i - 1];
            }
        }
        return returnValue;
    } 

    private void button1_Click(object sender, EventArgs e)
    {
        if (txtStr.Text.Trim() == "")
        {
            return;
        }
        else
        {
            if (txtStr.Text.Trim().Length > 16)
            {
                MessageBox.Show("数字金额太大!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (txtStr.Text.Trim().Substring(0, 1) == "0")
                {
                    MessageBox.Show("请正确输入金额!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    richTextBox1.Text = CNumToCh(txtStr.Text.Trim());
                }
            }
        }
    }

    private void txtStr_KeyPress(object sender, KeyPressEventArgs e)
    {
        if((e.KeyChar!=8&&!char.IsDigit(e.KeyChar))&&e.KeyChar!=13)
        {
            MessageBox.Show("请输入数字!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            txtStr.Text = "";
            txtStr.Focus();
        }
    }
}
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.label2 = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.button1 = new System.Windows.Forms.Button();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.txtStr = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(17, 53);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(65, 12);
        this.label2.TabIndex = 8;
        this.label2.Text = "转换结果:";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(17, 15);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(65, 12);
        this.label1.TabIndex = 7;
        this.label1.Text = "输入数字:";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(329, 11);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 5;
        this.button1.Text = "开始转换";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // richTextBox1
        // 
        this.richTextBox1.Location = new System.Drawing.Point(79, 53);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(325, 131);
        this.richTextBox1.TabIndex = 9;
        this.richTextBox1.Text = "";
        // 
        // txtStr
        // 
        this.txtStr.Location = new System.Drawing.Point(79, 12);
        this.txtStr.Name = "txtStr";
        this.txtStr.Size = new System.Drawing.Size(244, 21);
        this.txtStr.TabIndex = 6;
        this.txtStr.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtStr_KeyPress);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(414, 196);
        this.Controls.Add(this.richTextBox1);
        this.Controls.Add(this.txtStr);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.button1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "数字大小写转换";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.TextBox txtStr;
}

到此这篇关于C#实现数字转换汉字的示例详解的文章就介绍到这了,更多相关C#数字转汉字内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯