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
 

Rounding to integer

Previous Thread :: Next Thread 
NinjaShadow1 is not online. NinjaShadow1
Joined: 03 Mar 2009
Total Posts: 2689
27 Jan 2012 11:03 PM
How do I round a value to the nearest integer? (in a script)
Report Abuse
Jammintoad is not online. Jammintoad
Joined: 08 Feb 2009
Total Posts: 10239
27 Jan 2012 11:08 PM
I'm not sure if there's a method, but I would try using if v ​>= 5 then and if v < 5 then.
Report Abuse
CrazyBananaMonkey is not online. CrazyBananaMonkey
Joined: 10 Nov 2009
Total Posts: 1599
27 Jan 2012 11:11 PM
num1 = 2.5;
num1 = math.ceil(num1);


math.ceil rounds up, math.floor rounds down. You could make a function to detect which, ie:

function round(num)
if (math.ceil(num) - num) < (num - math.floor(num)) then
return math.ceil(num)
elseif (math.ceil(num) - num) > (num - math.floor(num)) then
return math.floor(num)
else
return math.ceil(num)
end

round(6.8)

> 7
Report Abuse
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
27 Jan 2012 11:11 PM
local decimal = 5.63
local int = math.floor(decimal+.5)
print(int)
Report Abuse
CrazyBananaMonkey is not online. CrazyBananaMonkey
Joined: 10 Nov 2009
Total Posts: 1599
27 Jan 2012 11:12 PM
Actually, you'd call the function with print(round(6.8)), and then you could do stuff like:

num1 = 6.7;
num1 = round(num1);
print(num1);

> 7
Report Abuse
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
27 Jan 2012 11:18 PM
if you want to round to a range use this (and no miz i did not test it):

function round(num,range) --range is optional, it will just convert to int if you dont put range in.
range = range or 1
return (num+(range/2))-((num+(range/2))%range)
end

print(round(364,100))


>400
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