Number (0.567) float once stored in datastore and back not staying 3 decimal points

i am working on a mmorpg and i want that items like armor and weapons have a float (so called purity indicator) this value will influence people lower or higher dmg boosts if its a better value

between 1 and 0 and it has 3 decimal points since roblox numberValue only support 3 else it rounds off
so example 0.001 , 0.123

problem is only one value 0.567 seems to become 0.566999999999 in the background this anoying as this will effect the difference between 0.567 and 0.566999 drasticly cause it upscales the buffs or decreases

am i missing somthing why this is happening

its on the image “pants”

format in datastore is type,id,amount,float
amount or float is nil aka void if its a non float or non stackable item

It shouldn’t be THAT bad, but the reason this happens is that there just isn’t a way to represent 0.567 with 1s and 0s, so it turns into the nearest one that can be represented, this thing;

It’s literally a difference of 0.000000000001, it shouldn’t affect your game unless you are multiplying it by like, 100 before even using it, in which case why don’t you just store that?

1 Like

maybe it better if i store a number between 0 and 1000 for example and adding a 0. in front aka devide it by 100 so its stores it as 456 → becomes in runtime 456/100 → 0.456 and if its 1000 → 1

would this be better this way i could add more decimal points as well as i feel 3 is not enough

also reason i am not doing 100 is for balancing reasons i set in a so called codex the expected base dmg and max dmg a weapon can have and if it need rebalancing later i can use that float again instead of rawly multiping atleast i think that easyier
yes u will prob have some people with float 0.1 0.2 to perhaps have same capped dmg cause the max set dmg thx for the help

NumberValues can store more than 3 decimal points

image

print(script.Value.Value)

image
But they only show the first 3.

? You can store in without decimals and then divide it when you load it in, but of course, the problem would still occur.

i already changed the concept being a purity indicator so oposide of a float higher the better so between 0 - and 1.000.000 of course it shows the percentage example 20.22% but once u hover over it will show the raw number i think that can work.

was taking too much inspiration from CSGO trying to make it work for me instead of coming with my own solution so there it is thx for the pointers and help i just woke up so its a bit wonky xD

Shouldn’t matter at all as that type of precision is mathematically equal. i.e 1.5999999999999999999999 is mathematically equal to 1.6. As explained by the solver (@Judgy_Oreo ) this is caused by something called a floating point error. He explained this basically; when a decimal doesn’t have enough bits to represent itself, it just truncates (rounds itself). This is the same reason why if you go out really really really far in roblox your body will start doing weird things and geometry becomes distorted. The actual positions of the geometry don’t have enough bits to represent themselves so it just rounds (which causes some error and shifts the geometry).
Read up on if you’re interested: What Every Computer Scientist Should Know About Floating-Point Arithmetic

1 Like
local value = 0.567

-- value is converted to 567
value = math.round(value * 1000)

-- now save value into datastore as 567
...

-- now load data from datastore
...

-- covert 567 back to 0.567
value /= 1000

print(value)

doing this will also have the benefit of using less characters in the datastore