|
| 17 Jan 2012 10:19 PM |
So I want to know how to round a number to the nearest whole number. Maybe something like this:
num = 1.2352
newnum = math.round(num)
print(newnum)
>1
Would that be correct? |
|
|
| Report Abuse |
|
|
| |
|
|
| 17 Jan 2012 10:21 PM |
function round(num) return math.floor(num +.5) end
print(round(3.6)) print(round(3.2)) print(round(3.5))
--> 4 --> 3 --> 4 |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:21 PM |
function round(num) math.floor(num+0.5) end
print(round(5.65))
>6 |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:22 PM |
| anyone got some butter for my latetoast? |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:23 PM |
Yep!
function round(x,inc) inc = not x==0 or inc or 1 return x < 0 and math.ceil(x/inc-.5)*inc or math.floor(x/inc+.5)*inc end
My code is even bettah! |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:30 PM |
dang i was about to do that next :(
function round(num,range) return (((num%range)*2)+num)-((((num%range)*2)+num)%range) end |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:31 PM |
| What does the % do? Never seen it used like that. |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:33 PM |
it's modulo, it gets the remainder of a fraction :3
3%2 -> 1 10%3 -> 1 15%6 -> 3 |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:35 PM |
| Ah, so math.floor(arg) rounds arg to the nearest whole number (or integer) below it, so you put + .5 to make it round normally. Thanks! |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:35 PM |
Huh. So when people were doing those decimal to fraction things....
UMG THEY GOT SCREWED! |
|
|
| Report Abuse |
|
|
|
| 17 Jan 2012 10:40 PM |
| So I need two other people to help me make sure my script is working (to make sure points are spread evenly), if anyone is willing to help me test that would be wonderful. Just follow me or play me "Keep Walking!" game. Thanks. |
|
|
| Report Abuse |
|
|
| |
|