亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解C#如何判斷輸入的數(shù)字是否符合貨幣格式

 更新時間:2024年01月18日 16:56:42   作者:wenchm  
這篇文章主要為大家詳細介紹了C#判斷輸入的數(shù)字是否符合貨幣格式的兩種常見方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

一、用正則表達式判斷輸入是否符合貨幣格式

// 判斷輸入是否貨幣合格
using System.Text.RegularExpressions;
namespace IsCurrency_Format
{
    partial class Program
    {
        static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);
 
            Console.WriteLine("請輸入要判斷的字符串(貨幣格式)");
            string input = Console.ReadLine()!.ToString();
 
            bool isValidFormat = IsCurrencyFormat(input);
 
            if (isValidFormat)
            {
                Console.WriteLine("該字符串是有效的貨幣格式!");
            }
            else
            {
                Console.WriteLine("該字符串不是有效的貨幣格式!");
            }    
            Console.ReadKey();// 等待按任意鍵結束程序
        }
 
        static bool IsCurrencyFormat(string input)
        {
            Regex regex = MyRegex();
            return regex.IsMatch(input);
        }
 
        [GeneratedRegex(@"^[+-]?\d+(,\d{3})*(\.\d{1,2})?$")]
        private static partial Regex MyRegex();
    }
}
//運行結果:
/*
請輸入要判斷的字符串(貨幣格式)
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 = "轉換結果:"
            };
            // 
            // 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))   //驗證輸入是否正確并賦值
            {
                NumberFormatInfo GN =new CultureInfo("zh-CN", false).NumberFormat;//實例化NumberFormatInfo對象
                GN.CurrencyGroupSeparator = ",";        //設置貨幣值中用來分組的字符串
                textBox2!.Text = temp.ToString("C", GN);//格式化為貨幣格式并顯示
            }
            else
            {
                MessageBox.Show("請輸入正確的貨幣值!", "提示!");
            }
        }
    }
}

到此這篇關于詳解C#如何判斷輸入的數(shù)字是否符合貨幣格式的文章就介紹到這了,更多相關C#判斷格式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論