|
| 04 Jun 2012 06:32 PM |
| In a game, i am dividing the number of players by 3 (ply/3) and sometimes you get a decimal. For this game i cannot have a decimal number, how do you round a number up or down? |
|
|
| Report Abuse |
|
|
RA2lover
|
  |
| Joined: 09 Nov 2008 |
| Total Posts: 1254 |
|
|
| 04 Jun 2012 06:34 PM |
math.floor/math.ceil
you can store stuff into IntValues too. |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 06:35 PM |
So like: numplayer/3 = number math.floor(number) would round it down? |
|
|
| Report Abuse |
|
|
nightname
|
  |
| Joined: 10 Jun 2008 |
| Total Posts: 8960 |
|
|
| 04 Jun 2012 06:37 PM |
function round(double) local int , dec = math.modf(double); if dec > 0.49 then return int + 1; end return int; end
-- Who needs math.ceil or math.floor? |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 06:50 PM |
@nightname, oh god why
>people still don't know this idiom
local function round(n) return math.floor(n+0.5); end
|
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
nightname
|
  |
| Joined: 10 Jun 2008 |
| Total Posts: 8960 |
|
|
| 04 Jun 2012 07:48 PM |
@Necro
I said no math.floor or math.ceil. |
|
|
| Report Abuse |
|
|
NVI
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 4744 |
|
|
| 04 Jun 2012 08:21 PM |
| Oh god, it took too long for someone to suggest math.floor(x + 0.5). |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 09:03 PM |
@NVI
Ikr? I even know that trick...
~Read Between The Squiggles~ |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 09:30 PM |
Here's the best way:
n = n - n % 1 i just pwned everyone in this thread with modulus. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 04 Jun 2012 09:31 PM |
| ^ Isn't that just math.floor? |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 09:32 PM |
@myrkos
It removes the decimal part of any number. It rounds the number. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 04 Jun 2012 09:35 PM |
That doesn't round it. It floors it.
The standard mathematical way of rounding (at least the classic way) is to add 0.5 and then floor it, which results in proper rounding. |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 09:37 PM |
@myrkos
There is no such thing as a standard mathematical way of rounding.
Look up 'Rounding' on Wikipedia. There are many ways to round a number.
Rounding can mean many different things. That's the reason there is no rounding function in Lua. There can't be a rounding function in Lua because there are many ways to round a number and they don't all give the same result. |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 04 Jun 2012 09:40 PM |
I shouldn't have said the standard way, but it is the classic way and the most widely accepted way. There is no reason for Lua not to have added a round function which used the algorithm which seemed to fit best.
Flooring simply is not a rounding method, or at least is a pretty crappy one. |
|
|
| Report Abuse |
|
|
|
| 04 Jun 2012 09:44 PM |
@myrkos
It is a rounding method, just like ceiling is. And that is actually the reason Lua doesn't have a rounding method. |
|
|
| Report Abuse |
|
|