Jump to content

_RoundTo UDF


Alek
 Share

Recommended Posts

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]

Link to comment
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.

Link to comment
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]

Link to comment
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.

Link to comment
Share on other sites

  • 2 weeks later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...