Link to home
Start Free TrialLog in
Avatar of pce
pce

asked on

ROUND function in VB

Example: round(10.5,0) gives as result 10. This is not a commercially correct rounding. The result has to be 11.
Solution ?????
ASKER CERTIFIED SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Private Function rounded(x As Double, places As Integer) As Double

    rounded = Int(x * 10 ^ places + 0.5) / 10 ^ places


End Function

Avatar of pce
pce

ASKER

The rounded function as you proposed works fine for our given example (13.5). However, if you try it as following :
a=rounded(13.35,1) the result is 13.3 instead of the expected 13.4.
We have changed the function :

Private Function rounded(x As Double, places As Integer) As Double
    rounded = x * 10 ^ places + 0.5
    rounded = Int(rounded) / 10 ^ places
End Function