Issues with multiplying values

I’m having issues with multiplying two string values together even though I did tonumber() to both of them. Here is a picture of the code:


Here are the values:
image
image
This is what the output returns:
image

I need to multiply the two values together. If I am doing this wrong, please correct me.

1 Like

The value of cashmultiply (it’s actually a leaderstat) is 1.03 as shown in the picture.

Is there a reason you’re using StringValue as opposed to NumberValue or IntValue?

NumberValue doesn’t store it correctly for some reason:
image
Integer values just don’t store decimals at all.

That’s just a rounding thing. Even if you convert a string to that number it may still come out like that.

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?

Use IntValue then, if I’m not wrong, IntValues doesn’t have decimals.

The thing is I need the decimal 1.03, just not 1.0000000000026 or anything like that.


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.

You can still store the multiplier as an int if you treat it as a percentage

local multiplier = [TheIntValueObject].Value -- 3 %
local income = 2500 * (1 + multiplier / 100)

But why does this code work, is there a difference between doing a tonumber(BasicCash) which returns a value of 2500 and typing out 2500?

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.

Hmm this seems to have fixed the problem:


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.

1 Like