'Rounding Up Fractional Numbers?
Q. How can I round up numbers to the nearst quarter in VB 5? Let`s say I have
the number 1.15, I would like it to round up to 1.25, and 1.27 would round up to
1.50.
A. Here`s a old rounding function that`s rather useful:
Result = (Fix((A * 10 ^ P + N/2)/N)*N)/10^P
Where:
A = the amount to round.
P = the number of decimal places. (2 in your case)
N = the rounding factor. (25 in your case)
Unfortunately, this function rounds up or down to the nearest n (actually
n/10^p).
To round up only, change the function to:
Result = (Fix((A * 10 ^ P + N - .1) / N) * N) / 10 ^ P
The -.1 in there is to keep exactly .50, say, from rounding up to .75.
Here`s a simple beginner`s function that will work pretty well for anybody:
Private Function RndByQtrs(Amt As Single) As Single
'Function assumes rounding to nearest
'quarter and two decimal places.
Dim Remainder As Single
'Get hundredths. Convert to integer (* 100).
Remainder = (Amt - Fix(Amt)) * 100
Select Case Remainder
Case 76 To 100
Remainder = 100
Case 51 To 75
Remainder = 75
Case 26 To 50
Remainder = 50
Case 1 to 25
Remainder = 25
End Select
RndByQtrs = Fix(Amt) + Remainder / 100
End Function
Thanks to: Bob Walsh
Downloads
In IE right-click and select 'Save Target As...' or in Netscape
right-click and select 'Save Link As...'
View Plain Text
Not Available
Not Available