Dynerov
|
  |
| Joined: 21 Jul 2014 |
| Total Posts: 14682 |
|
|
| 25 Jun 2015 04:37 PM |
round = (function(x, base) local y = x*(10^abs(base-1)) if y - floor(y) >= 0.5 then return (ceil(y)/abs(10^(base-1))) else return (floor(y)/abs(10^(base-1))) end end)
It returns the number, and base is technically just how many zeroes it has.
It can go VERY complex with big numbers, or very simple. It could make math.floor and math.ceil obsolete quickly!
Should I call it round10, with the more complex one being roundBase? |
|
|
| Report Abuse |
|
|
Dynerov
|
  |
| Joined: 21 Jul 2014 |
| Total Posts: 14682 |
|
|
| 25 Jun 2015 04:40 PM |
Feel free to use it. It's a bit heavy, yet I love it because you can customize it.
Here's the more complex (You can divide by numbers that can be different than 10)
And, whoops, it wouldn't work right up yet. Use math. before every math keyword. |
|
|
| Report Abuse |
|
|
|
| 25 Jun 2015 04:40 PM |
| local round = math.floor(initial number + 0.5) |
|
|
| Report Abuse |
|
|
|
| 25 Jun 2015 04:40 PM |
"Simplistic"
#OwnedByAbove
Enjoying your stay at the Scripters Forum? Join this! http://www.roblox.com/My/Groups.aspx?gid=2582784 |
|
|
| Report Abuse |
|
|
025110
|
  |
| Joined: 23 Nov 2012 |
| Total Posts: 57661 |
|
|
| 25 Jun 2015 04:41 PM |
function round(x) return math.floor(x + .5); end |
|
|
| Report Abuse |
|
|
Dynerov
|
  |
| Joined: 21 Jul 2014 |
| Total Posts: 14682 |
|
|
| 25 Jun 2015 04:42 PM |
roundBase = (function(x, base) local y = x*math.abs(base) if y - math.floor(y) >= 0.5 then return (math.ceil(y)/math.abs(base)) else return (math.floor(y)/math.abs(base)) end end)
round10 = (function(x, base) local y = x*(10^math.abs(base-1)) if y - math.floor(y) >= 0.5 then return (math.ceil(y)/math.abs(10^(base-1))) else return (math.floor(y)/math.abs(10^(base-1))) end end)
Feel free to use them, the roundBase is actually complex yet can be very precise when dividing for certain numbers, and the round10 is simply just rounding by tenths. |
|
|
| Report Abuse |
|
|
IoIiderp
|
  |
| Joined: 05 Feb 2012 |
| Total Posts: 8613 |
|
|
| 25 Jun 2015 04:43 PM |
function realisticRound(number) return"I am not gonna round this number for you, you lazy sack, do it yourself!" end
What I use in my scripts... And it works. |
|
|
| Report Abuse |
|
|
Dynerov
|
  |
| Joined: 21 Jul 2014 |
| Total Posts: 14682 |
|
|
| 25 Jun 2015 04:43 PM |
Well, look, that's simplistic, but mine is actually for variable precision.
You could find 8.92 in 8.9243, or just 8.9, or maybe you want 8.924. |
|
|
| Report Abuse |
|
|
|
| 25 Jun 2015 04:46 PM |
| string manipulation would also work |
|
|
| Report Abuse |
|
|
murcury57
|
  |
| Joined: 30 Jun 2010 |
| Total Posts: 90299 |
|
| |
|
|
| 25 Jun 2015 04:49 PM |
You can do this:
function round(n, b) return math.floor(n/b + 0.5) * b end
If you want to round to the nearest infinity, you could modify it like so:
function round(n, b) return math.floor(math.abs(n)/b + 0.5)* math.abs(n)/n * b end
n is the number, b is the base |
|
|
| Report Abuse |
|
|
Dynerov
|
  |
| Joined: 21 Jul 2014 |
| Total Posts: 14682 |
|
|
| 25 Jun 2015 04:51 PM |
Actually, Geo, I did make an encryption function with string manipulation.
It would be good for giving a top-secret message to someone. But the big problem is, that it can't encrypt symbols. |
|
|
| Report Abuse |
|
|