// 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";
}
}
Get ordinal suffix for a number
Page 1 of 11 Replies - 1596 Views - Last Post: 13 May 2009 - 07:19 PM
#1
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.
Replies To: Get ordinal suffix for a number
#2
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";
}
}
#3
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...=-}
Page 1 of 1

New Topic/Question
Reply



MultiQuote


|