Blinker. Posted March 4, 2015 Posted March 4, 2015 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.
Gallardo9944 Posted March 4, 2015 Posted March 4, 2015 (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 March 4, 2015 by Guest
Blinker. Posted March 4, 2015 Author Posted March 4, 2015 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
Mr.unpredictable. Posted March 4, 2015 Posted March 4, 2015 (edited) .... Edited March 6, 2015 by Guest
tosfera Posted March 4, 2015 Posted March 4, 2015 math.random That's a random number... not rounding a number to an integer.
Dealman Posted March 4, 2015 Posted March 4, 2015 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.
Blinker. Posted March 4, 2015 Author Posted March 4, 2015 No its a number , i gave just an example , it has a period not a comma on scoreboard
xXMADEXx Posted March 4, 2015 Posted March 4, 2015 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.
Dealman Posted March 4, 2015 Posted March 4, 2015 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))
Blinker. Posted March 4, 2015 Author Posted March 4, 2015 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
Dealman Posted March 4, 2015 Posted March 4, 2015 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.
Mr.unpredictable. Posted March 5, 2015 Posted March 5, 2015 What is math.round? never read about that in lua.
JR10 Posted March 6, 2015 Posted March 6, 2015 @unpredictable A useful function. https://wiki.multitheftauto.com/wiki/Math.round
Mr.unpredictable. Posted March 6, 2015 Posted March 6, 2015 @unpredictable A useful function. https://wiki.multitheftauto.com/wiki/Math.round thanks
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