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 an integer

Previous Thread :: Next Thread 
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
26 Apr 2014 02:24 PM
How?

So like 58 becomes 60

52 becomes 50

55 becomes 60

1 becomes 0


Not math.floor and math.ceil (which is for decimals to integers)
Report Abuse
gijsbel11 is not online. gijsbel11
Joined: 07 Feb 2009
Total Posts: 4223
26 Apr 2014 02:28 PM
make it a decimal, round it up, make it an integer

example:

n = 10--the magic rounding number
a = 73
a = math.floor((56/n)+0.5)*n
print(a)

when n = 10 then 'a' will be 70
when n = 100 then 'a' will be 100
when n = 20 then 'a' will be 80

etc...

Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
26 Apr 2014 02:29 PM
What -_-
Report Abuse
TheAltOfDavid12 is not online. TheAltOfDavid12
Joined: 05 Feb 2014
Total Posts: 352
26 Apr 2014 02:32 PM
function roundToTen(x)
local str = tostring(x)
local lastDig = tonumber(str:sub(str:len(), str:len()))
if not lastDig then return false end
if lastDig >= 5 then
x = x + (10 - lastDig)
else
x = x - lastDig
end
return x
end


That is what I came up with :P

I tested it and it works.
Report Abuse
gijsbel11 is not online. gijsbel11
Joined: 07 Feb 2009
Total Posts: 4223
26 Apr 2014 02:33 PM
Please don't respond like that, my answer was good.

the 'n' you want is 10 so

function 10round(n)
return math.floor(n/10+.5)*10
end

print(10round(27))
-> 30

Report Abuse
gijsbel11 is not online. gijsbel11
Joined: 07 Feb 2009
Total Posts: 4223
26 Apr 2014 02:33 PM
@TheAltOfDavid12

jesus christ my eyes
Report Abuse
yobo89 is not online. yobo89
Joined: 05 Jun 2010
Total Posts: 2341
26 Apr 2014 02:34 PM
Use the math.floor() method.
Report Abuse
TheAltOfDavid12 is not online. TheAltOfDavid12
Joined: 05 Feb 2014
Total Posts: 352
26 Apr 2014 02:34 PM
^ function names can't start with numbers

Mine works :P
Report Abuse
DrMathematica is not online. DrMathematica
Joined: 29 Aug 2008
Total Posts: 27268
26 Apr 2014 02:34 PM
"Please don't respond like that, my answer was good."

That judgement is reserved for the eyes of the OP.
Report Abuse
gijsbel11 is not online. gijsbel11
Joined: 07 Feb 2009
Total Posts: 4223
26 Apr 2014 02:37 PM
@TheAltOfDavid12

SO FINAL ANSWER, THREAD CLOSED, HERE NOTHING TO SEE, EXCEPT FOR OP

function tenround(n)
return math.floor(n/10+.5)*10
end

Report Abuse
Spongocardo is not online. Spongocardo
Joined: 06 Sep 2008
Total Posts: 2843
26 Apr 2014 02:46 PM
n = 10 --To the nearest... (E.G: nearest 10th, nearest 100th, etc.)
num = 52 --Any number you want to round up or down too.

function RoundNum()
number = num/n
if tonumber(string.sub(tostring(num), string.len(tostring(n))) < 5 then
number = math.ceil(number) * n
else
number = math.floor(number) * n
end
return number
end

print(RoundNum())

Here's my siggy... Done.
Report Abuse
gijsbel11 is not online. gijsbel11
Joined: 07 Feb 2009
Total Posts: 4223
26 Apr 2014 02:50 PM
@Spongocardo,

good answer but it's ugly and way to big so again :

SO, FINAL ANSWER, THREAD CLOSED, HERE NOTHING TO SEE, EXCEPT FOR OP

function tenround(n)
return math.floor(n/10+.5)*10
end
Report Abuse
Spongocardo is not online. Spongocardo
Joined: 06 Sep 2008
Total Posts: 2843
26 Apr 2014 02:52 PM
@gij
GG... XD

Here's my siggy... Done.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
26 Apr 2014 03:04 PM
Thanks guys. Also I am sorry for my response. I never understood your answer XD

I think I will use TheAltOfDavid12's as it is easy to understand. Thanks!
Report Abuse
Spongocardo is not online. Spongocardo
Joined: 06 Sep 2008
Total Posts: 2843
26 Apr 2014 03:05 PM
@war Glad you didn't use mine, mine fails.

Here's my siggy... Done.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
26 Apr 2014 03:05 PM
XD
Report Abuse
gijsbel11 is not online. gijsbel11
Joined: 07 Feb 2009
Total Posts: 4223
26 Apr 2014 03:05 PM
@warspyking,

"I think I will use TheAltOfDavid12's as it is easy to understand."

you've got to be kidding me

my answer is like 4 times as small and the working bit is only 1 line. It works 10 times faster and i have to say, mine is actually easier to understand.
If you don't understand mine just ask.
Report Abuse
Spongocardo is not online. Spongocardo
Joined: 06 Sep 2008
Total Posts: 2843
26 Apr 2014 03:07 PM
@gij Your script may be easier in your eyes, david's script may be easier in his eyes, but OP chose david's as it was easier in his eyes. Mine's just a mess xD

Here's my siggy... Done.
Report Abuse
gijsbel11 is not online. gijsbel11
Joined: 07 Feb 2009
Total Posts: 4223
26 Apr 2014 03:07 PM
Oh and even if you don't understand mine, just use it because it really is better for whole of roblox.
Report Abuse
morash is not online. morash
Joined: 22 May 2010
Total Posts: 5834
26 Apr 2014 03:12 PM
function Round(number, baseMultiple)
return math.floor((number/baseMultiple)+.5)*baseMultiple
end

print(Round(4, 10)) --0
print(Round(4, 5)) --5
print(Round(8, 5)) --10
print(Round(7.89, .1) --7.9

This will round to multiples of baseMultiple. If you just want to a certain spot you could use this function:

function Round(number, distanceFromRadix)
return math.floor((number/(10^distanceFromRadix)+.5)*(10^distanceFromRadix)
end

print(Round(4,1)) --0
print(Round(4,0)) --4
print(Round(8,2)) --0
print(Round(7.89, .1)) --7.9
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