SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    47 Post(s)
    Tagged
    0 Thread(s)

    Forcing a number to 2 decimal places in C#

    Hey guys

    On my website i have numbers that get calculated according to what the use types in. I need the number 2 be forced to 2 decimal places.

    At the moment im using Math.Round(decOne,2);

    that changes this: 65.231 into 65.23
    but if the user types in 65 it stays 65 and doesn't give me 65.00. Is there anyway to make the number to force 2 decimal places?

    I hope i explained this correctly.

    Thanks

  2. #2
    SitePoint Guru puco's Avatar
    Join Date
    Feb 2005
    Location
    Slovakia
    Posts
    785
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    .ToString ("0.00")
    Martin Pernecky

  3. #3
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    47 Post(s)
    Tagged
    0 Thread(s)
    Hey puco

    Thanks a lot for the quick reply. It works great.

    Thanks again

  4. #4
    S1tepoint Surfer drochili's Avatar
    Join Date
    Aug 2002
    Location
    Mississauga, Canada
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean to show it as decimal in a textbox or label right? I'm pretty sure that in your single or double variable, it will be as 65.00, but in the textbox/label, you can format it using toString.

    Hopefully this link helps:
    http://msdn.microsoft.com/library/de...matstrings.asp
    My Blog: DR's Corner
    Random babble: TheCoolerZone

  5. #5
    S1tepoint Surfer drochili's Avatar
    Join Date
    Aug 2002
    Location
    Mississauga, Canada
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whoops.. got beat to posting the reply
    My Blog: DR's Corner
    Random babble: TheCoolerZone


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •