|
| 06 Sep 2016 07:49 PM |
this is for an ingame currency system with withdraw/deposit.
I would use math.floor but they can keep depositing 0.5 and getting 1 for it, and generate cash.
Is there a way i can somehow make it use ONLY whole numbers?
ty |
|
|
| Report Abuse |
|
|
Xsitsu
|
  |
| Joined: 28 Jul 2009 |
| Total Posts: 2921 |
|
|
| 06 Sep 2016 07:52 PM |
Just don't let them deposit anything that isn't a whole number.
if (math.floor(number) ~= number) then error("you can't deposite non whole numbers!") end |
|
|
| Report Abuse |
|
|
|
| 06 Sep 2016 07:52 PM |
Ya you can check to see if a number is a whole number with this:
function checkWhole(input) return input % 1 == 0 end
that'll return true for numbers like 1 or 2, but false for 0.5 |
|
|
| Report Abuse |
|
|
L2000
|
  |
| Joined: 03 Apr 2008 |
| Total Posts: 77448 |
|
|
| 06 Sep 2016 07:57 PM |
How are you doing your math.floor? Its probably in the wrong place if its not working.
Alternatively, you could use string.format (I'm assuming Lua has it) to grab the whole number chunk of the number and add that
If you're seeing this post, it means I'm either dead (leaving behind a very fabulous looking corpse) or my soul has been captured by pixies! |
|
|
| Report Abuse |
|
|
rapmilo
|
  |
| Joined: 16 Aug 2011 |
| Total Posts: 69 |
|
|
| 06 Sep 2016 07:58 PM |
| Well if you want to round the number just try this: function round(number) if (number - (number % 0.1)) - (number - (number % 1)) < 0.5 then number = number - (number % 1) else number = (number - (number % 1)) + 1 end Honestly these are the few times where Lua lacking the equivalent of a int variable in Java is inconvenient. If you want to test the code...just type print(round(X)) where X is ### ###### of your choosing. Check if the rounding is correct and you're done. |
|
|
| Report Abuse |
|
|
|
| 06 Sep 2016 09:01 PM |
| string.format("%.2",123.239) |
|
|
| Report Abuse |
|
|
|
| 06 Sep 2016 09:59 PM |
| local wholeNumber, decimal = math.modf(Number) |
|
|
| Report Abuse |
|
|
rapmilo
|
  |
| Joined: 16 Aug 2011 |
| Total Posts: 69 |
|
|
| 07 Sep 2016 08:05 PM |
Here's a neater version: function round(number) if (number - (number % 0.1)) - (number - (number % 1)) < 0.5 then number = number - (number % 1) else number = (number - (number % 1)) + 1 end |
|
|
| Report Abuse |
|
|
|
| 07 Sep 2016 08:09 PM |
math.floor(num) That's all you need. The reason why 0.5 went to 1 is probably because you added 0.5 to num or you used math.ceil |
|
|
| Report Abuse |
|
|
|
| 07 Sep 2016 08:18 PM |
or maybe
thing = 123223233.222
print(tostring(thing):sub(1,math.max(tostring(thing):find("%.")) - 1))
not recommended and you should use math.floor/math.ceil
This siggy is copyrighted © |
|
|
| Report Abuse |
|
|