Jump to content

Lua division issue


Blueman

Recommended Posts

Posted

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. :|

Posted

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) 

Posted

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.

Posted

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.

Posted
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.

Posted

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) 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...