format textbox value as number

Discussion in 'ASP .Net' started by nic, Dec 16, 2003.

  1. nic

    nic Guest

    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
     
    nic, Dec 16, 2003
    #1
    1. Advertising

  2. 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/
    ----------------------------------
    Making waves on the Web


    "nic" <> wrote in message
    news:151d701c3c40b$1562b2a0$...
    > 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
     
    Rick Strahl [MVP], Dec 16, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    717
    Joe Kesselman
    Sep 19, 2006
  2. Ken Starks
    Replies:
    4
    Views:
    370
    Ken Starks
    Jun 23, 2008
  3. Chris Rebert
    Replies:
    1
    Views:
    415
    Grant Edwards
    Nov 5, 2010
  4. ruca

    Format textbox to time format

    ruca, May 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    939
    HJohns
    May 9, 2005
  5. Fei Liu
    Replies:
    21
    Views:
    2,485
    John Bokma
    Dec 16, 2006
Loading...

Share This Page