How do I get the last digit from a number?

Get help using Construct 2

Post » Thu Apr 06, 2017 5:52 pm

Hi, I'm trying to make a calendar inside my game which would show a number and after it have it say "st", "nd", "rd" and "th" based on the day.

So I thought that I could check to see what the last digit of the number is and then set the text to the whole number and then "st" or "nd" etc...
I would need to change this for 11,12 and 13 since these end with "th", but since they are only 3, it won't be such a hassle.

Are there any better ways of doing this?
If not, then how do I get the last digit from a number?

Thanks!
ImageImageImageImage
B
28
S
9
G
7
Posts: 229
Reputation: 5,518

Post » Thu Apr 06, 2017 6:25 pm

number%10 will get the last digit.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Thu Apr 06, 2017 6:52 pm

And this formula should append the correct suffix to the number.

Code: Select all
value & ((value % 100) - (value % 10) = 10 ? "th" : value % 10 = 1 ? "st" : value % 10 = 2 ? "nd" : value % 10 = 3 ? "rd" : "th")
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Sun Apr 09, 2017 7:54 am

R0J0hound wrote:number%10 will get the last digit.

Magistross wrote:And this formula should append the correct suffix to the number.

Code: Select all
value & ((value % 100) - (value % 10) = 10 ? "th" : value % 10 = 1 ? "st" : value % 10 = 2 ? "nd" : value % 10 = 3 ? "rd" : "th")

Thanks guys!
ImageImageImageImage
B
28
S
9
G
7
Posts: 229
Reputation: 5,518


Return to How do I....?

Who is online

Users browsing this forum: unxcellent and 4 guests