How do I get the last digit from a number?

  • 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!

  • number%10 will get the last digit.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • And this formula should append the correct suffix to the number.

    value & ((value % 100) - (value % 10) = 10 ? "th" : value % 10 = 1 ? "st" : value % 10 = 2 ? "nd" : value % 10 = 3 ? "rd" : "th")[/code:2ng1vp20]
  • number%10 will get the last digit.

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

    value & ((value % 100) - (value % 10) = 10 ? "th" : value % 10 = 1 ? "st" : value % 10 = 2 ? "nd" : value % 10 = 3 ? "rd" : "th")[/code:8adh9vq8]
    

    Thanks guys!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)