最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解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();// 等待按任意鍵結(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();
    }
}
//運行結(jié)果:
/*
請輸入要判斷的字符串(貨幣格式)
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))   //驗證輸入是否正確并賦值
            {
                NumberFormatInfo GN =new CultureInfo("zh-CN", false).NumberFormat;//實例化NumberFormatInfo對象
                GN.CurrencyGroupSeparator = ",";        //設(shè)置貨幣值中用來分組的字符串
                textBox2!.Text = temp.ToString("C", GN);//格式化為貨幣格式并顯示
            }
            else
            {
                MessageBox.Show("請輸入正確的貨幣值!", "提示!");
            }
        }
    }
}

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

相關(guān)文章

最新評論

定西市| 乐亭县| 大竹县| 新巴尔虎右旗| 津市市| 双桥区| 庆城县| 深水埗区| 景谷| 彰化市| 扎兰屯市| 锦屏县| 江安县| 策勒县| 贵港市| 新宁县| 运城市| 印江| 麻江县| 渭南市| 桑植县| 东城区| 黔南| 平江县| 吴旗县| 常德市| 英吉沙县| 乐至县| 威远县| 民县| 银川市| 老河口市| 东山县| 社会| 北海市| 客服| 扬州市| 宣化县| 慈溪市| 曲靖市| 洮南市|