詳解C#如何判斷輸入的數(shù)字是否符合貨幣格式
一、用正則表達(dá)式判斷輸入是否符合貨幣格式
// 判斷輸入是否貨幣合格 using System.Text.RegularExpressions; namespace IsCurrency_Format { partial class Program { static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); Console.WriteLine("請(qǐng)輸入要判斷的字符串(貨幣格式)"); string input = Console.ReadLine()!.ToString(); bool isValidFormat = IsCurrencyFormat(input); if (isValidFormat) { Console.WriteLine("該字符串是有效的貨幣格式!"); } else { Console.WriteLine("該字符串不是有效的貨幣格式!"); } Console.ReadKey();// 等待按任意鍵結(jié)束程序 } static bool IsCurrencyFormat(string input) { Regex regex = MyRegex(); return regex.IsMatch(input); } [GeneratedRegex(@"^[+-]?\d+(,\d{3})*(\.\d{1,2})?$")] private static partial Regex MyRegex(); } } //運(yùn)行結(jié)果: /* 請(qǐng)輸入要判斷的字符串(貨幣格式) 88888.88 該字符串是有效的貨幣格式! */
二、用double.TryParse()判斷輸入是否符合貨幣格式
//判斷輸入是否符合貨幣格式 using System.Globalization; namespace _051 { public partial class Form1 : Form { private GroupBox? groupBox1; private Button? button1; private TextBox? textBox2; private TextBox? textBox1; private Label? label2; private Label? label1; public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // button1 // button1 = new Button { Location = new Point(209, 65), Name = "button1", Size = new Size(75, 23), TabIndex = 4, Text = "判斷", UseVisualStyleBackColor = true }; button1.Click += Button1_Click; // // textBox2 // textBox2 = new TextBox { Location = new Point(77, 65), Name = "textBox2", Size = new Size(126, 23), TabIndex = 3 }; // // textBox1 // textBox1 = new TextBox { Location = new Point(77, 27), Name = "textBox1", Size = new Size(126, 23), TabIndex = 2 }; // // label2 // label2 = new Label { AutoSize = true, Location = new Point(6, 71), Name = "label2", Size = new Size(68, 17), TabIndex = 1, Text = "轉(zhuǎn)換結(jié)果:" }; // // label1 // label1 = new Label { AutoSize = true, Location = new Point(6, 30), Name = "label1", Size = new Size(68, 17), TabIndex = 0, Text = "輸入金額:" }; // // groupBox1 // groupBox1 = new GroupBox { Location = new Point(12, 12), Name = "groupBox1", Size = new Size(290, 117), TabIndex = 0, TabStop = false, Text = "是否符合貨幣格式" }; groupBox1.Controls.Add(button1); groupBox1.Controls.Add(textBox2); groupBox1.Controls.Add(textBox1); groupBox1.Controls.Add(label2); groupBox1.Controls.Add(label1); groupBox1.SuspendLayout(); // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(314, 141); Controls.Add(groupBox1); Name = "Form1"; StartPosition = FormStartPosition.CenterScreen; Text = "判斷是否符合貨幣格式"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); } private void Button1_Click(object? sender, EventArgs e) { if (double.TryParse(textBox1!.Text, out double temp)) //驗(yàn)證輸入是否正確并賦值 { NumberFormatInfo GN =new CultureInfo("zh-CN", false).NumberFormat;//實(shí)例化NumberFormatInfo對(duì)象 GN.CurrencyGroupSeparator = ","; //設(shè)置貨幣值中用來(lái)分組的字符串 textBox2!.Text = temp.ToString("C", GN);//格式化為貨幣格式并顯示 } else { MessageBox.Show("請(qǐng)輸入正確的貨幣值!", "提示!"); } } } }
到此這篇關(guān)于詳解C#如何判斷輸入的數(shù)字是否符合貨幣格式的文章就介紹到這了,更多相關(guān)C#判斷格式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
winform下實(shí)現(xiàn)win7 Aero磨砂效果實(shí)現(xiàn)代碼
winform下實(shí)現(xiàn)win7 Aero磨砂效果實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-03-03C#通過(guò)html調(diào)用WinForm的方法
這篇文章主要介紹了C#通過(guò)html調(diào)用WinForm的方法,涉及html頁(yè)面中使用JavaScript訪問(wèn)C#的相關(guān)技巧,需要的朋友可以參考下2016-04-04C#設(shè)計(jì)模式之裝飾器模式實(shí)例詳解
本文詳細(xì)講解了C#設(shè)計(jì)模式之裝飾器模式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10Visual Studio連接unity編輯器的實(shí)現(xiàn)步驟
unity編輯器中打開(kāi)C#腳本的時(shí)候發(fā)現(xiàn)Visual Studio沒(méi)有連接unity編輯器,本文主要介紹了Visual Studio連接unity編輯器的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-11-11C#讀取數(shù)據(jù)庫(kù)返回泛型集合詳解(DataSetToList)
本篇文章主要是對(duì)C#讀取數(shù)據(jù)庫(kù)返回泛型集合(DataSetToList)進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01C#實(shí)現(xiàn)經(jīng)典飛行棋游戲的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)經(jīng)典的飛行棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02