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
30
S
9
G
7
Posts: 260
Reputation: 5,642

Post » Thu Apr 06, 2017 6:25 pm

number%10 will get the last digit.
B
94
S
33
G
113
Posts: 5,354
Reputation: 73,269

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
74
S
31
G
26
Posts: 994
Reputation: 20,198

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
30
S
9
G
7
Posts: 260
Reputation: 5,642


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo and 12 guests