private void TextBox_TextChanged(object sender, EventArgs e)
{
string value = TextBox.Text.Replace(",", "");
long ul;
if (long.TryParse(value, out ul))
{
TextBox.TextChanged -= TextBoxTextChanged;
TextBox.Text = string.Format("{0:#,#0}", ul);
TextBox.SelectionStart = TextBox.Text.Length;
TextBox.TextChanged += TextBoxTextChanged;
}
}
我想通过在c#中按等号按钮在计算器中输入带小数值的逗号(例如:1234.1234至1,234.1234)
但它没有给出我渴望的结果。请帮助我解决这个问题吗?
答案 0 :(得分:0)
试试这个
TextBox.Text = ul.ToString("#,##0.0000");
答案 1 :(得分:0)
您需要修改事件的名称并稍微更改逻辑以处理.
之后没有任何数字(例如1.
),方法是在字符串中添加一个点。像这样:
private void TextBox_TextChanged(object sender, EventArgs e)
{
string value = TextBox.Text.Replace(",", "");
decimal ul;
if (decimal.TryParse(value, out ul))
{
TextBox.TextChanged -= TextBox_TextChanged;
if(ul % 1 == 0) // is the number integer
TextBox.Text = string.Format("{0:#,##0.#}", ul) + (TextBox.Text.EndsWith(".") ? "." : "");
else
TextBox.Text = string.Format("{0:#,##0.0#####}", ul) ;
TextBox.SelectionStart = TextBox.Text.Length;
TextBox.TextChanged += TextBox_TextChanged;
}
}