Jump to content

[Solved] Wrongly rounded numbers


ManeXi

Recommended Posts

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 by Guest
Link to comment

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 

Link to comment
  • Discord Moderators

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:

  1. Treat the number as a string, and transfer it to the client as a string.
  2. Let the client round the K/D ratio for display, instead of the server.

Link to comment

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