format textbox value as number

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
 
R

Rick Strahl [MVP]

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top