(Solved) Work around for Log10 rounding error

Hey fellow GDev’s.

Short Version:
Log10(1000) comes back with 2.999 instead of 3. Are that any workarounds I can use for this?

Long Version:
I am making a score for a game which has leading 0’s. Using a Substring I can grab 0’s and depending on the floored Log10 of my value I can find out how many less 0’s to grab. This works except when I hit exactly 1000. After trouble shooting I found that Math.Log has some rounding inaccuracies.

Edit* Current Work Around: instead of starting my value at 0 I start it at 0.1. Since I don’t use small numbers this works but is sloppy :stuck_out_tongue:

Any thoughts or direction is appreciated.
Thank you!

You could round the log number up with ceil(number) or

If the variable score is a number, you could use this to make it 10 places with leading zeros.

Set scoreText = StrRepeat(“0”, 10-StrLength( ToString(Variable(score)))) + ToString(Variable(score))

StrLength(string) gets the length of a string.

StrRepeat(string, number) repeats a string

(Some people use VariableString() instead of ToString(Variable()) for number variables. IDK if there’s a difference. I prefer ToString() for numbers and VariableString for strings because it helps me remember the variable’s type.)

Should work, as long as there are no rounding errors where it ends with X.0000001 (which there shouldn’t I only need up to 6 values)

Thanks for the quick answer!

Instead of ceil() you could use round().

I honestly just need StrLength since that’s what I was using Log10 for, to figure out the length of a variable number… But then I change it to a string anyways so this is perfect!

1 Like