This week we're going to write a function to turn a cardinal integer into an ordinal number (eg. 4 becomes 4th). This is mostly useful when generating a page to display to a visitor.
ASP
function ordinal(ByVal cardinal)
cardinal = CStr(cardinal)
if Right(cardinal, 1) = "1" and Right(cardinal, 2) <> "11" then
ordinal = cardinal & "st"
elseif Right(cardinal, 1) = "2" and Right(cardinal, 2) <> "12" then
ordinal = cardinal & "nd"
elseif Right(cardinal, 1) = "3" and Right(cardinal, 2) <> "13" then
ordinal = cardinal & "rd"
else
ordinal = cardinal & "th"
end if
end function
PHP
function ordinal($cardinal)
{
if (substr($cardinal, -1, 1) == 1 && substr($cardinal, -2, 2) != 11)
{
return ($cardinal . 'st');
}
elseif (substr($cardinal, -1, 1) == 2 && substr($cardinal, -2, 2) != 12)
{
return ($cardinal . 'nd');
}
elseif (substr($cardinal, -1, 1) == 3 && substr($cardinal, -2, 2) != 13)
{
return ($cardinal . 'rd');
}
else
{
return ($cardinal . 'th');
}
}
No comments:
Post a Comment