Sign in to follow this  
Followers 0
Alek

_RoundTo UDF

7 posts in this topic

#1 ·  Posted (edited)

A simple way of rounding a number to closest other number.

Func _RoundTo($iNum, $iTo)
    Return Round($iNum / $iTo) * $iTo
EndFuncoÝ÷ ØLZ^jëh×6Msgbox(0, "_RoundTo", _RoundTo(2, 5)) ;Should show 0 because 2 is closer to 0 (5*0) then 5 (5*1)
Msgbox(0, "_RoundTo", _RoundTo(3, 5)) ;Should show 5 because 3 is closer to 5 (5*1) then 0 (5*0)
Msgbox(0, "_RoundTo", _RoundTo(7, 5)) ;Should show 5 because 7 is closer to 5 (5*1) then 10 (5*2)
Msgbox(0, "_RoundTo", _RoundTo(8, 5)) ;Should show 10 because 8 is closer to 10 (5*2) then 5 (5*1)

Func _RoundTo($iNum, $iTo)
    Return Round($iNum / $iTo) * $iTo
EndFunc
Edited by Alek

[font="Impact"]Never fear, I is here.[/font]

Share this post


Link to post
Share on other sites



A simple way of rounding a number to closest other number.

Func _RoundTo($iNum, $iTo)
    Return Round($iNum / $iTo) * $iTo
EndFunc
Please write an example.

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Oh, so it rounds to the closest *multiple* of $iTo.

Share this post


Link to post
Share on other sites

A simple way of rounding a number to closest other number.

Func _RoundTo($iNum, $iTo)
    Return Round($iNum / $iTo) * $iTo
EndFuncoÝ÷ ØLZ^jëh×6Msgbox(0, "_RoundTo", _RoundTo(2, 5)) ;Should show 0 because 2 is closer to 0 (5*0) then 5 (5*1)
Msgbox(0, "_RoundTo", _RoundTo(3, 5)) ;Should show 5 because 3 is closer to 5 (5*1) then 0 (5*0)
Msgbox(0, "_RoundTo", _RoundTo(7, 5)) ;Should show 5 because 7 is closer to 5 (5*1) then 10 (5*2)
Msgbox(0, "_RoundTo", _RoundTo(8, 5)) ;Should show 10 because 8 is closer to 10 (5*2) then 5 (5*1)

Func _RoundTo($iNum, $iTo)
    Return Round($iNum / $iTo) * $iTo
EndFunc
If I want to round 7 to the closest number between 4 and 9, how can I do that?

How do you know what numbers to estimate?

You took the number 3 and you wanted to see if it is closer to 0 or 5. I saw that you specified number 5 but not the number 0.

Please explain me because I have not understand the algorithm.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Oh, so it rounds to the closest *multiple* of $iTo.

yes

If I want to round 7 to the closest number between 4 and 9, how can I do that?

How do you know what numbers to estimate?

You took the number 3 and you wanted to see if it is closer to 0 or 5. I saw that you specified number 5 but not the number 0.

Please explain me because I have not understand the algorithm.

as wraithdu said, its rounds your number ($iNum) to the closest $iTo Multiple

its almost like looking in the $iTo's multiplication table and finding the number closest to $iNum.

if $iNum = 13 and $iTo = 5 it will return 15 because 5 * 3

how the function works -> 13($iNum) / 5($iTo) = 2.6 -> Round(2.6) = 3 -> 3 * 5($iTo) = 15

dont now how to else to explain it :)


[font="Impact"]Never fear, I is here.[/font]

Share this post


Link to post
Share on other sites

yes

as wraithdu said, its rounds your number ($iNum) to the closest $iTo Multiple

its almost like looking in the $iTo's multiplication table and finding the number closest to $iNum.

if $iNum = 13 and $iTo = 5 it will return 15 because 5 * 3

how the function works -> 13($iNum) / 5($iTo) = 2.6 -> Round(2.6) = 3 -> 3 * 5($iTo) = 15

dont now how to else to explain it :)

Now I understand.

When the words fail... music speaks

Share this post


Link to post
Share on other sites

thank you. i found this quite usefull for a n level editor i am working on


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0