Math question - How to count number of digits

I’m working on making a timer that counts to the .1 second. I am doing it by converting frames (counted by an always trigger) to .1 second increments, then converting that to whole number seconds and .1 seconds. The problem is I have to have the whole number seconds and .1 seconds as two separate Labels, so I have to adjust the position of the .1 seconds label as the whole number label adds more digits.

My question is: How do I count the number of digits in a number? For example, 8 has one digit, 147 has three digits, and so on.

Alternatively, does anyone know of another way to implement a timer that counts to the .1 second place?

Well, I ended up just making filters for every place. So, it checks if the number is >= 0, >= 10, >= 100, etc. I would like a solution where it doesn’t error out after a certain point, but I suppose nobody will be playing a level for more than a couple of days at very very most anyway

@johnpost,
Timer behaviors are already in 1/10 (.1) intervals.
Just set a timer to 1 with repeat forever.


For finding how many digits there are, You can divide the number by 10 and also add 1 to a counter until the results = 0.

(It will reset to 1, but the last output is the number of digits in the number)

1 Like

Put this code in an expression:

Std.string(A).length

Input your number to the A input, then evaluate and it’ll output the number of digits. It will work like magic.

2 Likes

I tried using a timer at .1 before, and it didn’t seem to work very well. Converting frames to seconds works anyway, and it accounts for game lag, which I like.

The counter looks great, thanks!

This seems like what I want. The documentation page for the expression block only has pretty basic info, is there a list somewhere of all the functions we can use?

@johnpost You can use Haxe (a programming language) in the expression block. It’s probably too advanced to be explained in the documentation because most people won’t even understand it since they don’t have any programming experience. You can find a lot about Haxe in the website. Even about the code I wrote above (Std.string(A).length):

So there’s a lot you can do with an expression, but it’s limited to returning a single integer since that’s the only thing an expression allows as an output.

2 Likes

That’s amazing! So the expression block is quite powerful, but can only output an integer. Thank you for the info

1 Like

@johnpost Yes, exactly how you described it.

It might change in the future because grazer mentioned like a month ago that he’s going to work on allowing different data types like text, lists and floating points. We’re most likely going to get new behavior blocks for that to make it less complicated. But that’s all unimportant for now. You can keep an eye on it.

1 Like