C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#判断格式

详解C#如何判断输入的数字是否符合货币格式

作者:wenchm

这篇文章主要为大家详细介绍了C#判断输入的数字是否符合货币格式的两种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

一、用正则表达式判断输入是否符合货币格式

// 判断输入是否货币合格
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#如何判断输入的数字是否符合货币格式的文章就介绍到这了,更多相关C#判断格式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文