Results 1 to 10 of 10

Thread: Need help with rounding numbers.

  1. #1

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Need help with rounding numbers.

    I understand the concept of rounding I guess, all I know is math.round(), but I need a break down of it so I can round a number like this: 14.789543 to this: 14.8 because after the 7 there is an 8, which means you would round the seven up. Can anyone tell me how this is done?
    And some more detail, I need to round to the nearest tenth.

  2. #2
    New Member
    Join Date
    Jan 2009
    Location
    South Wales
    Posts
    12

    Re: Need help with rounding numbers.

    There is second optional parameter that can be sent to this function that returns it to a decimal point. So...

    math.round(14.789543, 1) would return 14.8.

  3. #3

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Re: Need help with rounding numbers.

    Quote Originally Posted by sra3cf View Post
    There is second optional parameter that can be sent to this function that returns it to a decimal point. So...

    math.round(14.789543, 1) would return 14.8.
    Thaty works somewhat, the only problem is that It's not rounding the decimal, it's rounding to the nearest whole digit.
    I need it to round to the nearest tenth.

  4. #4

    Re: Need help with rounding numbers.

    Well are you storing the result as an Integer? Integers cannot hold Decimal places.
    This is wrong:
    vb.net Code:
    1. Dim x As Integer = Math.Round(14.7859, 1)
    2. 'This will remove the decimal!!!
    This is more logical:
    vb.net Code:
    1. Dim x As Double = Math.Round(14.7859, 1)
    2. 'This will not!
    Doubles are designed to perform math operations, and the Math.Round() Function returns a double.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Need help with rounding numbers.

    Decimal will work better than Double too...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Re: Need help with rounding numbers.

    Alright, I figured out my problem, my problem was that I was rounding a number from a textbox, so I was using CInt when I should have been using CDbl. Now can anyone tell me how to round to the nearest hundredth?

  7. #7
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Need help with rounding numbers.

    Someone already did, have you tried changing 2nd parameter of math.round at all?
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  8. #8

    Re: Need help with rounding numbers.

    Quote Originally Posted by techgnome View Post
    Decimal will work better than Double too...

    -tg
    Forgot about Decimal. I just usually use Double because I've never had a need to use it yet..

  9. #9

    Thread Starter
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Re: Need help with rounding numbers.

    Before I can mark this resolved, how could I round to say, the nearest 10, like if the number is 56, it will round up to 60.

  10. #10
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Need help with rounding numbers.

    divide by 10, round, multiply by 10
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

Posting Permissions

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



Click Here to Expand Forum to Full Width