简体   繁体   中英

Calculator - thousand separator by comma in c#

private void TextBox_TextChanged(object sender, EventArgs e)
{
string value = TextBox.Text.Replace(",", "");
double dbl;
if (double.TryParse(value, out dbl))
{
    TextBox.TextChanged -= TextBoxTextChanged;
    TextBox.Text = string.Format("{0:#,#0}", dbl);
    TextBox.SelectionStart = TextBox.Text.Length;
    TextBox.TextChanged += TextBoxTextChanged;
}

}

I used above code for making Calculator. I want to get results comma with decimal value. I want to type 1,234.1234 in the

textBox, but I can not type 1,234.1234 in the Text Box. I mean comma with decimal value not getting.

Can anybody kindly please help me to solve this problem ?

You have to provide a Culture that uses . as thousands sign. Normally you want to use the users current culture.

double.TryParse(value, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.CurrentCulture, out dbl)

Try this:

int value = 300000
String.Format("{0:#,###0}", value);
// will return 300,000

http://msdn.microsoft.com/en-us/library/system.string.format.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM