format textbox value as number

  • Thread starter Thread starter nic
  • Start date Start date
N

nic

Hi,
I have a textbox value that I need to force (and format)
to be a numeric value. For example, I want to allow a
user to type 1000 or 1,000 or 1,000,000 but not something
that is not numberic. Essentially I want to ignore the
commas. If I use the compareValidator it does not allow
commas. Is there an easy way to do this? Also I need to
format a string such as 1000 as 1,000 (without any decimal
places) When I format 1000 as a numeric it returns
1,000.00.

Thanks
 
Hi Nic,

You can use Format strings to do the output generation. ToString("C")
generates numbers with the thousands separators, but unfortunately also with
the $. You can also build custom format strings, but offhand I can't
remember the format - look in the MSDN docs.

On the way in you can use decimal.Parse() with options that strip that stuff
out. I use the following in my DataBinding Controls which do the conversion
automatically.

Decimal.Parse( (string) loValue,NumberStyles.Any);

This doesn't address the client side Validators. For that you probably will
neeed to use the RegEx validation control.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/blog/
 
Back
Top