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: Rounding number to nearest whole number

Previous Thread :: Next Thread 
1MangoMan0 is not online. 1MangoMan0
Joined: 28 Sep 2010
Total Posts: 1596
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
1MangoMan0 is not online. 1MangoMan0
Joined: 28 Sep 2010
Total Posts: 1596
17 Jan 2012 10:20 PM
er... title fix
Report Abuse
SecureDatabase is not online. SecureDatabase
Joined: 12 Dec 2010
Total Posts: 392
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
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
17 Jan 2012 10:21 PM
function round(num)
math.floor(num+0.5)
end

print(round(5.65))

>6
Report Abuse
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
17 Jan 2012 10:22 PM
anyone got some butter for my latetoast?
Report Abuse
SecureDatabase is not online. SecureDatabase
Joined: 12 Dec 2010
Total Posts: 392
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
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
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
SecureDatabase is not online. SecureDatabase
Joined: 12 Dec 2010
Total Posts: 392
17 Jan 2012 10:31 PM
What does the % do? Never seen it used like that.
Report Abuse
blueymaddog is not online. blueymaddog
Joined: 23 Sep 2009
Total Posts: 5459
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
1MangoMan0 is not online. 1MangoMan0
Joined: 28 Sep 2010
Total Posts: 1596
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
SecureDatabase is not online. SecureDatabase
Joined: 12 Dec 2010
Total Posts: 392
17 Jan 2012 10:35 PM
Huh. So when people were doing those decimal to fraction things....

UMG THEY GOT SCREWED!
Report Abuse
1MangoMan0 is not online. 1MangoMan0
Joined: 28 Sep 2010
Total Posts: 1596
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
1MangoMan0 is not online. 1MangoMan0
Joined: 28 Sep 2010
Total Posts: 1596
17 Jan 2012 10:44 PM
Never mind.
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