JSONDecode floating point inaccuracy

What’s the issue?
The :JSONDecode() function does not return the exact number that it did.

More Details

The JSON code:

{
    "Name": "Classic Sword",
    "ItemType": "Weapon",
    "Description": "Test sword description",
    "ImageId": "[REDACTED]",
    "MaxQuantity": 1,
    "Properties": {
        "Damage": {
            "Speed": 1,
            "Min": 10,
            "Max": 15,
            "BiasFactor": 0.56
        }
    }
} //comment

Output:

        ▼  {
    ["Description"] = "Test sword description",
    ["ImageId"] = "[REDACTED]",
    ["ItemType"] = "Weapon",
    ["MaxQuantity"] = 1,
    ["Name"] = "Classic Sword",
    ["Properties"] =  ▼  {
        ["Damage"] =  ▼  {
            ["BiasFactor"] = 0.5600000000000001,
            ["Max"] = 15,
            ["Min"] = 10,
            ["Speed"] = 1
        }
    }
}

this one? im not a computer engineer but programs have their own way to store datas and numbers

0.56 isnt an INT value but a FLOAT
( float values are decimal numbers, like 1.5, 8.9, 0.78)
and these are not the same thing, and are stored differently

in short, dont worry, this is just a way roblox handles decimals

Thank you for the clarification but I know primitive types.


I mean :JSONDecode just reads the string, I don’t think there should be a problem or something.

Note: I tried other floats and 0.56 really messes it up. What a coincidence…

LUA doesnt use variable names, example

on C++ and other languages, to make a variable you have to:

float Floatnumber = 1.5f
int  Intnumber = 2
string text = "Hello World"
bool debounce = true

Lua instead, does this automatically

floatNumber = 1.5
IntNumber = 2
text = "hello World"
debounce = true

cool right? the negative effect is that lua is a high level language, meaning that this eats alot of information (ram), to make the language easy to understand, but lags

this is an useless information for roblox but is cool to know

wait, i thing i wrote something you didnt asked

im gonna delete this account bye

The issue is due to how computers store decimal numbers. When you decode JSON, small differences can happen because of the way floating-point numbers are represented. This is normal and happens because 0.56 isn’t stored exactly as 0.56 in memory. It’s just how computers handle decimal numbers.

1 Like

Welp, there goes my ["BiasFactor"]

I mean you could just round the number after decoding the JSON

I don’t think its a problem with JSONDecode but just a problem with how Roblox prints numbers. The classic 0.2 + 0.1 for example, if printed with
print(0.2 + 0.1)
would return 0.300000000000000004

The exact same issue happens with the javascript engine in your browser.
console.log(0.2 + 0.1) also doesn’t correct the floating point number.

Not really…

Why not? What’s the issue with doing it like that

I don’t really have a problem with the slight inaccuracy.
But I’m marking this thread as a solution. <!–for now.>

Cause (probable): Floating-point arithmetic - Wikipedia