Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

 

'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 View Plain Text
Download Demo Project Not Available
View Code Online Not Available





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.