Need an Explanation ?

Milan

Registered User.
Local time
Today, 09:45
Joined
Feb 17, 2002
Messages
85
Hi People,
I have the function below that rounds up or down . Code works fine, but i want to be clear in my head of what the function is actually doing?.

Public Function MyRound(Byval YourNumber as Double, Places as Integer)as Double

Myround = Yournumber * (10 ^ Places) Not sure about this ?
MyRound = MyRound + 0.5 ' This Adds 0.5
MyRound = Int(MyRound) ' This Converts To Integer whole No
MyRound = MyRound / (10 ^ Places) ' Not Sure about this?

can somebody explain the one's i have marked , what is the code specifially doing?.

Thanks In advance for Any Help Here!
 
It is rounding a number to x decimal places (...places as integer)

The maths is saying I want to round eg 4.7980 to 2 decimal places. To do this, it muliplies the figure by 100 so moving it across by 2 places, rounds the figure and the returns it to 2dp. In the code this is done by saying multiply by 10 to the power of x (gives the 100) where x is the number of decimal places.

So for the above: 4.7980 * 10 ^ 2 (10 to the power of 2) = 479.80
Round the number: int(479.8 + 0.5) = 480
Return to what is was: 480 / (10 ^ 2) = 4.80

HTH
 
Thanks for that !! Much clearer!!!
 

Users who are viewing this thread

Back
Top Bottom