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: Whole number

Previous Thread :: Next Thread 
peaspod is not online. peaspod
Joined: 29 Sep 2010
Total Posts: 2809
14 Nov 2012 04:07 PM
How do I round a value to a whole number?

I know this isn't right but here is an example:
toWholeNumber(decimalNumber)
Report Abuse
zars15 is not online. zars15
Joined: 10 Nov 2008
Total Posts: 9999
14 Nov 2012 04:11 PM
number = 4.6
number2 = 7.3


function round(arg)
if type(arg) == "number" then
a,b = math.modf(arg)
if b < 0.5 then
return math.floor(arg)
elseif b >= 0.5 then
return math.ceil(arg)
end
end
end

print(round(number))
print(round(number2))
Report Abuse
XxChopSlaperxX is not online. XxChopSlaperxX
Joined: 09 Nov 2010
Total Posts: 3112
14 Nov 2012 04:13 PM
function round(n, p)
return string.format("%0."..p.."f", n)
end

nate890 made that
Report Abuse
Solotaire is not online. Solotaire
Joined: 30 Jul 2009
Total Posts: 30356
14 Nov 2012 04:14 PM
math.floor(val) will round down.
math.ceil(val) will round up.

If you want to round a number based on the tens place, then do something such as:

if val + .5 - math.floor(val) == 0 then
     val = math.floor(val)
else
     val = math.ceil(val)
end

Not 100% sure of the code I just wrote, but it should be a decent concept behind it at least.
Report Abuse
zars15 is not online. zars15
Joined: 10 Nov 2008
Total Posts: 9999
14 Nov 2012 04:15 PM
@chop. So what exactly are n and p?
Report Abuse
MBGAME is not online. MBGAME
Joined: 10 Oct 2012
Total Posts: 5
14 Nov 2012 04:45 PM
function Round(Int,Factor)
return math.floor(Int/Factor+.5)*Int
end

Round(1,10) > 0
Round(5,10) >10
Round(12,10) > 10
Round(100,10)>100
Round(10,20) > 20
Round(1,20) > 0
Round(30,20) > 40
Report Abuse
1Topcop is not online. 1Topcop
Joined: 09 Jun 2009
Total Posts: 6635
14 Nov 2012 04:47 PM
I made this a couple months ago,

function Round(N,T)
if(T)then
return Round(N/T)*T
else
return math.floor(N+.5)
end
end

Round(.6) > 1
Round(.2) > 0
Round(2,10) > 0
Round(6,10) > 10
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