ManeXi Posted June 12, 2016 Posted June 12, 2016 (edited) I use a function that should work rightly on rounding numbers, but for some reason it rounds the numbers wrongly, instead of putting 0.56 it puts 0.569999999999999999999999 or 0.56000000000000000000000000001 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 The number is displayed in the scoreboard. Edited June 12, 2016 by Guest
Discord Moderators AlexTMjugador Posted June 12, 2016 Discord Moderators Posted June 12, 2016 (edited) What number are you trying to round? Rounding 0.56 under default parameters works fine for me. Edited June 12, 2016 by Guest
Castillo Posted June 12, 2016 Posted June 12, 2016 You can set the amount of decimals you want it to return. Like this: math.round ( 0.569999999999999999999999, 2 )
ManeXi Posted June 12, 2016 Author Posted June 12, 2016 I'm rounding player's KDR function setkdr ( player ) local dea = getElementData(player,"Deaths") local kil = getElementData(player,"Kills") local kdr = kil/dea setElementData(player,"K/D Ratio", math.round(kdr, 2)) local acc = getPlayerAccount(player) if not isGuestAccount(acc) then setAccountData(acc,"K/D Ratio", math.round(kdr, 2)) end end
Discord Moderators AlexTMjugador Posted June 12, 2016 Discord Moderators Posted June 12, 2016 You are probably experiencing some kind of event number transfer precision issue. I believe that this is the same issue that makes MTA: SA editor save numbers with a ridiculously high decimal count, even if you only use two or three decimals. You can workaround that limitation in two ways: Treat the number as a string, and transfer it to the client as a string. Let the client round the K/D ratio for display, instead of the server.
ManeXi Posted June 12, 2016 Author Posted June 12, 2016 (edited) And the result is...? An example right now would be: Kills = 232 Deaths = 395 result is: 0.587341 so rounded should be 0.59 but instead of it it puts 0.5899997 Edited June 12, 2016 by Guest
Castillo Posted June 12, 2016 Posted June 12, 2016 Strange, I ran the code on: http://www.lua.org/cgi-bin/demo And the output was: 0.59.
ManeXi Posted June 12, 2016 Author Posted June 12, 2016 Strange, I ran the code on: http://www.lua.org/cgi-bin/demoAnd the output was: 0.59. http://i.imgur.com/X59pOcm.jpg
Tomas Posted June 12, 2016 Posted June 12, 2016 Strange, I ran the code on: http://www.lua.org/cgi-bin/demoAnd the output was: 0.59. http://i.imgur.com/X59pOcm.jpg setElementData(player,"K/D Ratio", tostring(math.round(kdr, 2)))
ManeXi Posted June 12, 2016 Author Posted June 12, 2016 Seems that setting it as string fixed it, thanks alot <3
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