weewoo5
|
  |
| Joined: 25 Oct 2009 |
| Total Posts: 2281 |
|
|
| 15 May 2013 08:14 PM |
What would I do in scripting to round a number to the nearest whole.
I am converting string bytes back into char after being encrypted so the values have decimals, which won't end up getting you anything with string.char. |
|
|
| Report Abuse |
|
|
Praecedo
|
  |
| Joined: 27 Dec 2011 |
| Total Posts: 308 |
|
|
| 15 May 2013 08:16 PM |
math.ceil(number here) + 0
|
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:22 PM |
Math.ceil increases the number to the nearest whole number, so if you're trying to convert 1.05 to 1, don't use math.ceil.
Math.floor just drops the decimals -- if you wanted to round 6.87 to 7, don't use math.floor.
If you want something accurate, try something like this:
function roundNumber(number)--number would be the number you're converting num = number - math.floor(number) if num >= 0.5 then return(math.ceil(number)) else return(math.floor(number)) end end |
|
|
| Report Abuse |
|
|
Praecedo
|
  |
| Joined: 27 Dec 2011 |
| Total Posts: 308 |
|
|
| 15 May 2013 08:24 PM |
| Echo is right, I was just too lazy to write that script :P |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:34 PM |
math.floor(num+.5)
That's the simplest way. |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:35 PM |
@Echo
Round -2.1 with your function. It goes to -3. ;) |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:37 PM |
:/ Correct
I am now inefficient... Go with awesome's code. |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:38 PM |
| Awesome's code rounds -2.5 to -2. |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:41 PM |
:/
I rage
I don't think that the OP's numbers will be negative anyway, so he should be fine. |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:42 PM |
function round(num) return math.floor( num + (num<0 and -0.5 or 0.5) ) end |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:43 PM |
Er...
function round(num) return (num<0) and math.ceil(num - 0.5) or math.floor(num + 0.5) end
My above function was used in one of my games and does not correctly round. XD |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:43 PM |
| And that's why you're a better scripter than me :) |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 08:44 PM |
| I would've never though to make it that simple. |
|
|
| Report Abuse |
|
|
weewoo5
|
  |
| Joined: 25 Oct 2009 |
| Total Posts: 2281 |
|
|
| 15 May 2013 08:58 PM |
| I saw one of those scripts, but I would need to change the variables like 50 times for my encryption algorithm. |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 09:19 PM |
Ive been struggling with this too, but surprising as it sounds, I think strings maybe the answer XD.
Try this:
function round(num) local numstring = tostring(num) local x,y = string.find(num,"." if x and y then -- find the dot, I may be using this wrong not good with string.find/match XD. local num2 = string.sub(num,y+1) if num2 >=5 then num3 = tonumber(string.sub(num,y-1))+1 return num3 -- maybe error here since code after return? :P elseif num2 < 5 then num3 = tonumber(string.sub(num,y-1)) return num3 end end end
You better appreciate this lol, I had to really think about this XD.
-- cardgamechampion/AlternativeAcc |
|
|
| Report Abuse |
|
|
|
| 15 May 2013 09:24 PM |
Though the use of strings is an option, they are not essential for this. :D Great work, though. :) |
|
|
| Report Abuse |
|
|
|
| 17 May 2013 10:17 AM |
Thanks. I never got to test my code. Does it work? :P
(At line 3 I forgot a parenthesis for string.find LOL). |
|
|
| Report Abuse |
|
|
|
| 17 May 2013 10:19 AM |
varM = 3.1
math.floor(varM+.5)
varM = varM - varM - varM
uh lol
|
|
|
| Report Abuse |
|
|
|
| 17 May 2013 10:35 AM |
num = 2.5
numN = math.floor(num+.5)
numN = numN - numN- numN
print (numN) |
|
|
| Report Abuse |
|
|