1 Replies - 1596 Views - Last Post: 13 May 2009 - 07:19 PM

#1 Nayana   User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 32
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Get ordinal suffix for a number

Posted 28 January 2008 - 10:20 PM

Description: No includes needed. These suffixes are only relevant in English. Just implement when printing a string. E.g. printf("This is the %d%s apple", 3, ordinalSuffix(3));If 1, 2, 3, 21, 22, 23 etc. are sent than "st", "nd", or "rd" are returned. Otherwise "th" is returned. Works on negative and positive numbers.
// Returns either "st", "nd", "rd" or "th" depending on the number.
// e.g. int t = 21; printf("%d%s", t, ordinalSuffix(t));
// will print: "21st"
const char* ordinalSuffix(int n) {
  // make n always positive. This is faster than having to check for
  // negative cases.
  n = n < 0 ? -n : n; // same as n = abs(n); but faster, so there.
  
  // Numbers from 11 to 13 don't have st, nd, rd
  if(10 < n && n < 14) return "th";

  switch(n % 10) {
    case 1:
      return "st";

    case 2:
      return "nd";

    case 3:
      return "rd";

    default:
      return "th";
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Get ordinal suffix for a number

#2 Nayana   User is offline

  • DIC Hawk - 나야나 नयन:
  • member icon

Reputation: 32
  • View blog
  • Posts: 824
  • Joined: 14-November 07

Re: Get ordinal suffix for a number

Posted 28 January 2008 - 10:20 PM

Description: No includes needed. These suffixes are only relevant in English. Just implement when printing a string. E.g. printf("This is the %d%s apple", 3, ordinalSuffix(3));If 1, 2, 3, 21, 22, 23 etc. are sent than "st", "nd", or "rd" are returned. Otherwise "th" is returned. Works on negative and positive numbers.
// Returns either "st", "nd", "rd" or "th" depending on the number.
// e.g. int t = 21; printf("%d%s", t, ordinalSuffix(t));
// will print: "21st"
const char* ordinalSuffix(int n) {
  // make n always positive. This is faster than having to check for
  // negative cases.
  n = n < 0 ? -n : n; // same as n = abs(n); but faster, so there.
  
  // Numbers from 11 to 13 don't have st, nd, rd
  if(10 < (n%100) && (n%100) < 14) return "th";

  switch(n % 10) {
    case 1:
      return "st";

    case 2:
      return "nd";

    case 3:
      return "rd";

    default:
      return "th";
  }
}

Was This Post Helpful? 0
  • +
  • -

#3 clarence_cool03   User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 13-May 09

Re: Get ordinal suffix for a number

Posted 13 May 2009 - 07:19 PM

{-='COoL prOgRam... 'Would you minD f i ask u a favOr??.. 'Do u knOw hOw tO prOgram a grocery or supermarket prOgram??... 'pLs heLP me pLs... 'f u knOw 8 jUz send 8 2 my emaiL pLs [email protected] 'tHank's...=-}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1