【问题标题】:As the user types: Separate numbers with commas, and format it into currency in C#当用户键入时:用逗号分隔数字,并在 C# 中将其格式化为货币
【发布时间】:2015-09-20 02:16:24
【问题描述】:

我有一个名为 textBox1 的文本框。

目标:一旦用户输入 textBox1,我希望程序将数字转换为货币格式。

示例:如果用户键入 123456,我希望程序像这样分隔数字 123,456。

【问题讨论】:

    标签: c# .net winforms textbox currency


    【解决方案1】:

    经过研究,我发现了这段代码。这段代码正是我想要的。

        private void form_3_Load(object sender, EventArgs e)
        {
            textBox1.Text = "$0.00";
        }
    
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ///
            //Remove previous formatting, or the decimal check will fail including leading zeros
            string value = textBox1.Text.Replace(",", "")
                .Replace("$", "").Replace(".", "").TrimStart('0');
            decimal ul;
            //Check we are indeed handling a number
            if (decimal.TryParse(value, out ul))
            {
                ul /= 100;
                //Unsub the event so we don't enter a loop
                textBox1.TextChanged -= textBox1_TextChanged;
                //Format the text as currency
                textBox1.Text = string.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:C2}", ul);
                textBox1.TextChanged += textBox1_TextChanged;
                textBox1.Select(textBox1.Text.Length, 0);
            }
            bool goodToGo = TextisValid(textBox1.Text);
            btn_test.Enabled = goodToGo;
            if (!goodToGo)
            {
                textBox1.Text = "$0.00";
                textBox1.Select(textBox1.Text.Length, 0);
            }
            ///
        }
    
        private bool TextisValid(string text)
        {
            Regex money = new Regex(@"^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");
            return money.IsMatch(text);
        }
    
    
        void tb_TextChanged(object sender, EventArgs e)
        {
            //Remove previous formatting, or the decimal check will fail
            string value = textBox1.Text.Replace(",", "").Replace("$", "");
            decimal ul;
            //Check we are indeed handling a number
            if (decimal.TryParse(value, out ul))
            {
                //Unsub the event so we don't enter a loop
                textBox1.TextChanged -= tb_TextChanged;
                //Format the text as currency
                textBox1.Text = string.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:C2}", ul);
                textBox1.TextChanged += tb_TextChanged;
            }
        }
    

    【讨论】:

      【解决方案2】:

      下面是基本的做法,当文本发生变化时将其转换为十进制,然后将文本更改为十进制的字符串表示形式。

      textBox1.TextChanged += (s,e) =>
      {
         var value = Decimal.Parse(textBox1.Text);
         textBox1.Text = value.ToString("C");
      }
      

      您还应该检查文本框中的非法数字。看看Decimal.TryParse

      【讨论】:

        【解决方案3】:

        我知道已经晚了,但试试这个应该可以解决你的问题

        private void textBox1_TextChanged(object sender, EventArgs e)
                {
                    if (textBox1.Text.Length > 0)
                    {
                        textBox1.Text = Convert.ToDouble(textBox1.Text).ToString("N0");
                        textBox1.SelectionStart = textBox1.Text.Length;
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 2012-04-21
          • 2012-08-18
          • 2012-07-26
          • 1970-01-01
          • 2022-01-07
          • 1970-01-01
          • 1970-01-01
          • 2011-12-04
          • 1970-01-01
          相关资源
          最近更新 更多