C#實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字的示例詳解
更新時(shí)間:2022年12月18日 08:30:09 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實(shí)踐過程
效果
代碼
public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static string CNumToCh(string x) { //數(shù)字轉(zhuǎn)換為中文后的數(shù)組 string[] num = new string[] { "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖" }; //為數(shù)字位數(shù)建立一個(gè)位數(shù)組 string[] digit = new string[] { "", "拾", "佰", "仟" }; //為數(shù)字單位建立一個(gè)單位數(shù)組 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; //外層循環(huán),四位一組,每組最后加上單位: ",萬億,",",億,",",萬," for (int i = k; i > 0; i--) { int L = 4; if (i == k && m != 0) L = m; //得到一組四位數(shù) string four = x.Substring(finger, L); int l = four.Length; //內(nèi)層循環(huán)在該組中的每一位數(shù)上循環(huán) for (int j = 0; j < l; j++) { //處理組中的每一位數(shù)加上所在的位 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; //每組最后加上一個(gè)單位:",萬,",",億," 等 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("數(shù)字金額太大!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (txtStr.Text.Trim().Substring(0, 1) == "0") { MessageBox.Show("請(qǐng)正確輸入金額!", "警告", 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("請(qǐng)輸入數(shù)字!", "警告", 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 = "轉(zhuǎn)換結(jié)果:"; // // 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 = "輸入數(shù)字:"; // // 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 = "開始轉(zhuǎn)換"; 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 = "數(shù)字大小寫轉(zhuǎn)換"; 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; }
到此這篇關(guān)于C#實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字的示例詳解的文章就介紹到這了,更多相關(guān)C#數(shù)字轉(zhuǎn)漢字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

WPF實(shí)現(xiàn)授權(quán)碼顯示密文并支持換行
這篇文章主要為大家詳細(xì)介紹了如何使用WPF實(shí)現(xiàn)授權(quán)碼顯示密文并支持換行,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下
2024-10-10 
C#實(shí)現(xiàn)簡(jiǎn)單的天氣預(yù)報(bào)示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的天氣預(yù)報(bào)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
2020-06-06