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: Round Numbers (Math Function)

Previous Thread :: Next Thread 
AleksF1992 is not online. AleksF1992
Joined: 22 Aug 2008
Total Posts: 118
05 Feb 2012 11:40 AM
Hello, I took my time making a function for rounding out numbers! Because I started a project thinking it might be a function math.round(num) I could use, but NO... Apparently that's a function that was placed in the folder of concidered additions according to the wiki.
So here you go:

--------------------------------------------------------------------------------------------
function Math_Round(number) --Returns a value with less decimals!
local scale_low = -6 --Smallest number. (Ex. -6)
local scale_top = 6 --Largest number. (Ex. 6)
local interval = 0.1 --The interval you want returns. (Ex. 3.14 = 3.1)

local diff = scale_top - scale_low --How many numbers that have to be checked.
local range = interval / 2 --To check weither a [number] is in the range or not.
local rounded = 0 --The value to return. (0 if untouched)
local low = scale_low - range --[number] MUST be larger.
local top = scale_low + range --[number] MUST be less.
local current = scale_low --The value it's currently checking if is closest.

for x = 1, diff * (1 / interval) do
if rounded == 0 then
if number > low and number < top then
rounded = current
else
low = low + interval
top = top + interval
current = current + interval
end
end
end

return rounded --Returns the [rounded] number. (Ex. 3.1)
end

--This function was made by: AleksF1992
--(You can remove this if you like, but please don't claim it as your own!)
--------------------------------------------------------------------------------------------

I'm usually bored, so tell me if you have a challenger or problem you can't solve, and I'll see what I can do. ^^
Report Abuse
noobkid is not online. noobkid
Joined: 17 Sep 2007
Total Posts: 649
05 Feb 2012 11:41 AM
There's math.floor which rounds the number down

Ruined your work :P
Report Abuse
Cobblestoner is not online. Cobblestoner
Joined: 01 Jan 2012
Total Posts: 70
05 Feb 2012 11:41 AM
Thanks, I'll come back to this if I ever need something for rounding. I've had incidents where I've needed them before.
Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
05 Feb 2012 11:58 AM
function Round(Number)

local Int, Dec = math.modf(Number)

if Dec > 0.5 then
Int = Int + 1
end

return Int
end

print(Round(1.7))
Report Abuse
AleksF1992 is not online. AleksF1992
Joined: 22 Aug 2008
Total Posts: 118
05 Feb 2012 11:59 AM
I know, but mine is quite better than math.floor() since it only rounds down to WHOLE numbers... Mine does as many decimals you wish.
And it doesn't always round down either, it takes it to the closest. ^^

Guess you can call mine a editable math.floor =)
Report Abuse
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
05 Feb 2012 12:02 PM
You could do that, or:

math = {round = function(num,decimalplaces)
n = 10^decimalplaces
return math.floor(num*n+.5)/n
end}

math.round(5.73,1)

--> 5.8
Report Abuse
AleksF1992 is not online. AleksF1992
Joined: 22 Aug 2008
Total Posts: 118
05 Feb 2012 12:04 PM
*Darkmist, that would return whole numbers as math.floor i think =P
I made my code cause I needed decimals...
It's math.floor with a round up function implemented ^^
Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
05 Feb 2012 12:06 PM
Much better then mine D:
Report Abuse
Darkmist101 is not online. Darkmist101
Joined: 30 Jul 2010
Total Posts: 6208
05 Feb 2012 12:07 PM
I know mine only returns decimals...

I don't like straining myself :3
Report Abuse
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
05 Feb 2012 12:07 PM
Yeah, look at mine. It rounds to the nearest number and you can change how many decimal places you want to return.
Report Abuse
AleksF1992 is not online. AleksF1992
Joined: 22 Aug 2008
Total Posts: 118
05 Feb 2012 12:19 PM
Your is easier than mine to use for sure, but I'm having trouble to understand how math.modf works... (know it's the remainder of a division, but when you say modf(3.14) what does it mod WITH? is it a constant or something?) as well as the rest of the calculation x)
I'm not a genious in math, but i the good think with codes is that it does the practical for you ^^
Report Abuse
AleksF1992 is not online. AleksF1992
Joined: 22 Aug 2008
Total Posts: 118
05 Feb 2012 12:23 PM
Also, I apperently fail in english x)
Report Abuse
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
05 Feb 2012 12:32 PM
Returns the integral and fractional parts of x

math.modf(3.14)

--> 3 and 0.14
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