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
|
  |
| 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 |
|
|
|
| 14 Nov 2012 04:13 PM |
function round(n, p) return string.format("%0."..p.."f", n) end
nate890 made that |
|
|
| Report Abuse |
|
|
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
|
  |
| Joined: 10 Nov 2008 |
| Total Posts: 9999 |
|
|
| 14 Nov 2012 04:15 PM |
| @chop. So what exactly are n and p? |
|
|
| Report Abuse |
|
|
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
|
  |
| 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 |
|
|