Results 1 to 12 of 12

Thread: **RESOLVED** Rounding to the nearest whole number

  1. #1

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721

    **RESOLVED** Rounding to the nearest whole number

    Hi

    I need help! I have conducted a search on this forum and have
    found nothing.... what I need help with is rounding to the nearest
    whole number ???

    i.e
    1.5 becomes 2.0
    1.4 becomes 1.0

    etc etc

    Your help and time is very much appreciated

    Last edited by holly; Mar 23rd, 2004 at 10:22 AM.
    ** HOLLY **

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Take a look at the Round() function.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    Hi Thanks for replying correct me if I'm wrong but
    I thought the round function was used to display a value by
    x amount of decimal places...



    edit....I've tried the round function and it appears to be rounding
    down even when the value is i.e 1.5
    Last edited by holly; Mar 23rd, 2004 at 08:36 AM.
    ** HOLLY **

  4. #4
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Round(12.6, 0)
    One thing to note, it rounds x.5 down to x
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  5. #5

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    Hi,
    I need to round x.5 up


    Thanks for all your help

    ** HOLLY **

  6. #6
    Hyperactive Member Bloged's Avatar
    Join Date
    May 2001
    Location
    Rotterdam, The Netherlands
    Posts
    330
    One thing to note, it rounds x.5 down to x
    Just add 0.1 to the number you want to round before applying Round()

    Grtz,

    Bloged

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    See if this works for you
    VB Code:
    1. Private Sub Command1_Click()
    2.     Me.Print RoundNumber(1.5)
    3.     Me.Print RoundNumber(2.5)
    4.     Me.Print RoundNumber(1)
    5.     Me.Print RoundNumber(45)
    6.     Me.Print RoundNumber(-0.001)
    7. End Sub
    8.  
    9. Private Function RoundNumber(ByVal Num As Double) As Long
    10.     If Num - Fix(Num) <> 0 Then
    11.         Num = Num + 0.5
    12.     End If
    13.    
    14.     RoundNumber = Round(Num)
    15. End Function

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    As the Round function rounds to the nearest even number, it is best to remove it, and use Int instead (as it always rounds down). Adding 0.5 ensures that it will always round .5 or above up, and anything less than .5 down.
    VB Code:
    1. Private Function RoundNumber(ByVal Num As Double) As Long
    2.  
    3.     RoundNumber = Int(Num + 0.5)
    4.  
    5. End Function

  9. #9
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718
    I had this when programming an estimates program a few months back.

    It's because we all use mathematical rounding, and VB uses bankers rounding! Pain in the arse, but heh...

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Here's a full alternative to the Round function using standard mathematical rounding (allows you specify the number of decimal places to round to):
    VB Code:
    1. Private Function RoundNumber(ByVal Num As Double, _
    2.                              Optional ByVal DecimalPlaces As Integer = 0)
    3.  
    4.   If DecimalPlaces = 0 Then
    5.     RoundNumber = Int(Num + 0.5)
    6.   Else
    7. Dim Multiplier As Long
    8.     Multiplier = 10 ^ DecimalPlaces
    9.  
    10.     RoundNumber = (Int((Num * Multiplier) + 0.5)) / Multiplier
    11.   End If
    12.  
    13. End Function

  11. #11

    Thread Starter
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    Thanks everyone for your help and time...its very much
    appreciated
    ** HOLLY **

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    si is on the right track, but I think it warrants further explanation as to why the use of Round in VB is undesireable. This comes from some testing we've done here on this issue.

    There are two kinds of rounding, Banker rounding and Scientific rounding. Banker rounding is the rounding type that we've all been taught in math. 0-4 Round down, while 5-9 round up. In scientific, it's a bit more complicated. 0-4 round down, while 6-9 round up. When it is rounding 5, it rounds to the nearest even number. 1.5 rounds to 2 while 2.5 also rounds to 2. This is the style that ROUND uses in VB. It sucks, we had to write our own rounder function to get VB to do it right. Now, MS in their infinite wisdom, uses banker rounding. Ugh!

    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??? *

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