首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

文本框显示数字格式?该如何处理

2013-11-08 
文本框显示数字格式?怎样设置?1,234,567,890.55[解决办法]2500000.ToString(N)2,500,000.00[解决办法]us

文本框显示数字格式?
怎样设置?
1,234,567,890.55
[解决办法]
2500000.ToString("N")

2,500,000.00
[解决办法]
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public class Form1 : Form
  {
    TextBox tb = new TextBox();
    public Form1()
    {
      tb.TextChanged += new EventHandler(tb_TextChanged);
      Controls.Add(tb);
    }

    void tb_TextChanged(object sender, EventArgs e)
    {
      string value = tb.Text.Replace(",", "");
      ulong ul;
      if (ulong.TryParse(value, out ul))
      {
        tb.TextChanged -= tb_TextChanged;
        tb.Text = string.Format("{0:#,#}", ul);
        tb.SelectionStart = tb.Text.Length;
        tb.TextChanged += tb_TextChanged;
      }
    }
  }
}


http://social.msdn.microsoft.com/Forums/en-US/98887be8-814c-47e1-8110-c2442ce1640b/how-to-seprate-digits-with-comma-in-textbox
[解决办法]
private void textBox1_Leave(object sender, EventArgs e)
{
      int num = int.Parse(textBox1.Text);
      textBox1.Text = string.Format(num.ToString("N"));
}

热点排行