generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: How do I round a number to the nearest whole

Previous Thread :: Next Thread 
weewoo5 is not online. 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 is not online. Praecedo
Joined: 27 Dec 2011
Total Posts: 308
15 May 2013 08:16 PM
math.ceil(number here) + 0

Report Abuse
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
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 is not online. 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
TheAwesomenessDude is not online. TheAwesomenessDude
Joined: 17 Jul 2011
Total Posts: 11602
15 May 2013 08:34 PM
math.floor(num+.5)

That's the simplest way.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
15 May 2013 08:35 PM
@Echo


Round -2.1 with your function. It goes to -3. ;)
Report Abuse
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
15 May 2013 08:37 PM
:/ Correct

I am now inefficient... Go with awesome's code.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
15 May 2013 08:38 PM
Awesome's code rounds -2.5 to -2.
Report Abuse
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
15 May 2013 08:42 PM
function round(num)
return math.floor( num + (num<0 and -0.5 or 0.5) )
end
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
15 May 2013 08:43 PM
And that's why you're a better scripter than me :)
Report Abuse
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
15 May 2013 08:44 PM
I would've never though to make it that simple.
Report Abuse
weewoo5 is not online. 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
AlternativeAcc is not online. AlternativeAcc
Joined: 29 Mar 2012
Total Posts: 1337
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
AlternativeAcc is not online. AlternativeAcc
Joined: 29 Mar 2012
Total Posts: 1337
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
pokemon771 is not online. pokemon771
Joined: 28 Oct 2008
Total Posts: 8671
17 May 2013 10:19 AM
varM = 3.1

math.floor(varM+.5)

varM = varM - varM - varM

uh
lol


Report Abuse
pokemon771 is not online. pokemon771
Joined: 28 Oct 2008
Total Posts: 8671
17 May 2013 10:35 AM
num = 2.5



numN = math.floor(num+.5)

numN = numN - numN- numN

print (numN)
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image