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
;) :p
Printable View
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
;) :p
Take a look at the Round() function. :)
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
Round(12.6, 0)
One thing to note, it rounds x.5 down to x
Hi,
I need to round x.5 up
Thanks for all your help
;) ;)
Just add 0.1 to the number you want to round before applying Round()Quote:
One thing to note, it rounds x.5 down to x
Grtz,
Bloged
See if this works for youVB 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
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
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... ;)
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
Thanks everyone for your help and time...its very much
appreciated ;) ;)
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