How do I get the last digit from a number?

Get help using Construct 2

» 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!
B
31
S
9
G
7
Posts: 262
Reputation: 5,671

» Thu Apr 06, 2017 6:25 pm

number%10 will get the last digit.
B
97
S
36
G
131
Posts: 5,511
Reputation: 83,460

» 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
80
S
33
G
27
Posts: 1,027
Reputation: 21,114

» 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!
B
31
S
9
G
7
Posts: 262
Reputation: 5,671

Who is online

Users browsing this forum: No registered users and 22 guests