简体   繁体   中英

Decimal Thousand Separator and Decimal Point

I googled the web for hours but couldn't find my answer. I need when a user types or copies any number in TextBox then the number should follow the bellow rules:

  1. Only one decimal point should be permitted.
  2. Thousand Separator.
  3. Trailing zeros should be removed. I mean the zeros after decimal point should NOT be displayed.

Update For example,

123 => 123

1234.00 => 1,234

123456.05 => 123,456.05

123456.50 => 123,456.5

How can I do this?

The easiest way would be to try to parse value as a number.

decimal value;
if(decimal.TryParse(textBox.Text, out value)
{
   //value is ok
   textBox.Text = string.Format("{0}", value);//If you want some fancier formatting
}
else
{
    //Value is not valid
}

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