I suggest separately storing your values and displays then. The actual numbers being stored in number format, and then updating the StringValue with your formatted version.
Otherwise you’ll encounter issues like this where there might be some small detail that causes tonumber to return nil.
But I can’t seem to find any other way to store the decimal correctly (1.03). The decimal updates to the player’s cash multiplier leaderstat, so it can be 1.03, 1.66, 2, etc
Regardless whether the output looks like 1.03, if it is being displayed with the additional rounding error, any calculation will also be done with that same rounded number due to floating point rounding errors. This is not something you can avoid by converting a string into a number at the last minute.
If you really, really, really can’t live like this, you can use arbitrary precision but you would have to implement this yourself. Even then, I’m pretty sure any decimal with 0.03 tacked on will cause rounding error (it’s not a nice number in binary).
It worked in this code though for some reason. Instead of storing 2500 in a string, I just typed it out. The cashmultiplier is still 1.03 though. Why does it work here but not in the previous code? Is it the value that stores “2500” the one that’s tripping me up?
It somehow worked in that code, that I made a while ago. cashmultiplier is still a string value with the decimal 1.03, all I changed was typing out 2500 instead of storing it in a value.
I believe Lua converts strings into numbers automatically when you call a mathematical operation on it, like
local a = "1.03"
local b = a + 0
b = b*2
print(b) --> should output 2.06
Regardless, you should probably resort to using a number value. Like I said above, the rounding will occur in the calculation, regardless of what the output of a string value shows you.
It seems like using a variable to get the value then multiplying the variables didn’t work but getting the value directly and multiplying it directly without using a variable worked.
Not really, they’re both being assigned as numbers. The only difference is that tonumber() has a chance of returning nil if the string cannot be parsed into a number. Also it’s just adding unnecessary lines of code.
But like @tralalah said, you can’t really avoid the floating point error, and it may also be worth noting that .0000000000000000266 is an extremely negligible difference. (2.66 * 10^-17). It is so small that my TI-84 won’t even display the change if I add it to a large number like 2500.