|
| 21 Jun 2015 11:56 PM |
| How do I use math.floor or math.ceil to successfully round a float number (such as a transparency value) accurately? The problem I face is that the numbers sometimes round down while others round up. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 22 Jun 2015 12:00 AM |
function _G.round(n) n = tostring(n) local decimal = string.find(n, ".", 1, true) return n:sub(0, decimal + 1) end
print(_G.round(50.5132423)) -- Will print "50.5" print(_G.round(10.43)) -- Will print "10.4" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Jun 2015 12:01 AM |
that is gross, please don't use string manip. to do number stuff. If he want to round to the 1/10's place, just do (math.floor((num * 10) + .5) / 10) |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 22 Jun 2015 12:03 AM |
| cnt. Wasn't my script. I just found a free model :) |
|
|
| Report Abuse |
|
|