Alek Posted December 12, 2008 Share Posted December 12, 2008 (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 December 12, 2008 by Alek [font="Impact"]Never fear, I is here.[/font] Link to comment Share on other sites More sharing options...
Andreik Posted December 12, 2008 Share Posted December 12, 2008 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. Link to comment Share on other sites More sharing options...
wraithdu Posted December 12, 2008 Share Posted December 12, 2008 Oh, so it rounds to the closest *multiple* of $iTo. Link to comment Share on other sites More sharing options...
Andreik Posted December 12, 2008 Share Posted December 12, 2008 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 More sharing options...
Alek Posted December 12, 2008 Author Share Posted December 12, 2008 Oh, so it rounds to the closest *multiple* of $iTo.yesIf 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 Multipleits 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 * 3how the function works -> 13($iNum) / 5($iTo) = 2.6 -> Round(2.6) = 3 -> 3 * 5($iTo) = 15dont now how to else to explain it [font="Impact"]Never fear, I is here.[/font] Link to comment Share on other sites More sharing options...
Andreik Posted December 12, 2008 Share Posted December 12, 2008 yesas wraithdu said, its rounds your number ($iNum) to the closest $iTo Multipleits 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 * 3how the function works -> 13($iNum) / 5($iTo) = 2.6 -> Round(2.6) = 3 -> 3 * 5($iTo) = 15dont now how to else to explain it Now I understand. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
corgano Posted December 23, 2008 Share Posted December 23, 2008 thank you. i found this quite usefull for a n level editor i am working on 0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now