Blueman Posted June 17, 2011 Posted June 17, 2011 I was thinking before writing a code and I found a Issue it appears. If I wrote a code to divide a players money by 40 it would return a decimal in some cases and I need it to be a whole number. Is there any way to convert a decimal to a whole number in lua.
Blueman Posted June 17, 2011 Author Posted June 17, 2011 I'll try that, and thanks. Edit: How exactly do you use tonenumber Google and the Lua guide offer little.
karlis Posted June 17, 2011 Posted June 17, 2011 roundednum=math.floor(number*(10^digitCountAfterDot)+0.5)/(10^digitCountAfterDot)
JR10 Posted June 17, 2011 Posted June 17, 2011 Or just use this function function math.round(number, decimals, method) decimals = decimals or 0 local factor = 10 ^ decimals if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor else return tonumber(("%."..decimals.."f"):format(number)) end end --now use it on any number like this math.round(number)
AGENT_STEELMEAT Posted June 17, 2011 Posted June 17, 2011 Or, you could use: math.floor(float) --Rounds DOWN to the nearest whole number --or math.ceil(float) --Rounds UP to the nearest whole number Same as what JR10 posted but a little simpler, just depends on whether you want to be a little generous or a little stingy.
JR10 Posted June 17, 2011 Posted June 17, 2011 Well, why when you got a function that will just make it integer math.round is better.
AGENT_STEELMEAT Posted June 17, 2011 Posted June 17, 2011 Not necessarily - your function has 3 arguments, and in the end the scripter is going to have to choose between "ceil" or "floor", the only thing that is slightly more convenient is the decimal option, but for his case he just needs to round to a whole number.
Blueman Posted June 17, 2011 Author Posted June 17, 2011 Wow I go to bed and wake up with more responses then I estimated I would have and thanks you all helped.
JR10 Posted June 17, 2011 Posted June 17, 2011 Not necessarily - your function has 3 arguments, and in the end the scripter is going to have to choose between "ceil" or "floor", the only thing that is slightly more convenient is the decimal option, but for his case he just needs to round to a whole number. No, the 2 last arguments are optional incase you want to use it ciel or floor leave em blank to use round method.
Callum Posted June 17, 2011 Posted June 17, 2011 You can simply use this to round to the nearest number (note a string is returned, so use tonumber if needed); string.format("%.0f",numberVariable)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now