|
-
Mar 23rd, 2004, 08:19 AM
#1
Thread Starter
Fanatic Member
-
Mar 23rd, 2004, 08:28 AM
#2
Not NoteMe
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. 
-
Mar 23rd, 2004, 08:30 AM
#3
Thread Starter
Fanatic Member
-
Mar 23rd, 2004, 08:37 AM
#4
Not NoteMe
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. 
-
Mar 23rd, 2004, 08:41 AM
#5
Thread Starter
Fanatic Member
-
Mar 23rd, 2004, 08:42 AM
#6
Hyperactive Member
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
-
Mar 23rd, 2004, 08:53 AM
#7
See if this works for you
VB Code:
Private Sub Command1_Click()
Me.Print RoundNumber(1.5)
Me.Print RoundNumber(2.5)
Me.Print RoundNumber(1)
Me.Print RoundNumber(45)
Me.Print RoundNumber(-0.001)
End Sub
Private Function RoundNumber(ByVal Num As Double) As Long
If Num - Fix(Num) <> 0 Then
Num = Num + 0.5
End If
RoundNumber = Round(Num)
End Function
-
Mar 23rd, 2004, 09:26 AM
#8
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:
Private Function RoundNumber(ByVal Num As Double) As Long
RoundNumber = Int(Num + 0.5)
End Function
-
Mar 23rd, 2004, 10:04 AM
#9
Fanatic Member
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...
-
Mar 23rd, 2004, 10:17 AM
#10
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:
Private Function RoundNumber(ByVal Num As Double, _
Optional ByVal DecimalPlaces As Integer = 0)
If DecimalPlaces = 0 Then
RoundNumber = Int(Num + 0.5)
Else
Dim Multiplier As Long
Multiplier = 10 ^ DecimalPlaces
RoundNumber = (Int((Num * Multiplier) + 0.5)) / Multiplier
End If
End Function
-
Mar 23rd, 2004, 10:22 AM
#11
Thread Starter
Fanatic Member
-
Mar 23rd, 2004, 10:31 AM
#12
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|