|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
| |
|
|
| 05 Feb 2012 12:07 PM |
I know mine only returns decimals...
I don't like straining myself :3 |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 05 Feb 2012 12:23 PM |
| Also, I apperently fail in english x) |
|
|
| Report Abuse |
|
|
|
| 05 Feb 2012 12:32 PM |
Returns the integral and fractional parts of x
math.modf(3.14)
--> 3 and 0.14 |
|
|
| Report Abuse |
|
|