Jump to content

math.round


Blinker.

Recommended Posts

Posted

Hello ,

i've got no idea why that's not working , it shows on the column "R" the value '4,5012304213' with so much numbers .

i'm using that :

  
setElementData(v,"R",math.round((select[1].r),2) 
  
  

i've tried also tonumber and still not working and no errors in debug ,

thanks in advance.

Posted (edited)

math.round doesn't seem to exist in original Lua. Use this function:

function math.round(val) 
  return math.ceil(val-0.5) 
end 

You can round this to 2 last diggits like this:

local num = math.round(val*100)/100 

Or just use this function:

function math.round(val,todiggit) 
  local todiggit = todiggit or 0 
  return math.ceil((val-0.5)*(10^todiggit))/(10^todiggit) 
end 

Untested though

Edited by Guest
Posted

i've added it already

  
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 
  

Posted

Is it a number or a string? Seeing as how it doesn't work and your float value has a comma instead of a period - I'll go ahead and assume it's a string.

I assume that function doesn't work with strings.

Posted

Put this in your code and post what it outputs:

outputChatBox ( "Type: " .. type ( select[1].r ), root ); 
 outputChatBox ( "Value: " .. tostring ( select[1].r ), root ); 

And, if debugscript says any errors, please post those too.

Posted

Well since you're not giving us enough information, go ahead and try this;

setElementData(v, "R", tostring(math.round(tonumber(select[1].r)), 2)) 

Posted

Hello ,

sorry i wasn't home , i was on my phone.

@ xXMADEXx i did what u said , i got

Type: number

Value: 0.42857098579407

and no errors in debug

@Dealman

i got number 0 and number 1 and number 2 in scoreboard without period when my ratio increases

Posted

Since it is a number, then this should work just fine;

setElementData(v, "R", tostring(math.round(select[1].r, 2))) 

That should show up as 0.42.

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