Re: Ordinal Numbers (1st, 2nd, etc.)
Re: Ordinal Numbers (1st, 2nd, etc.)
- Subject: Re: Ordinal Numbers (1st, 2nd, etc.)
- From: Nigel Garvey <email@hidden>
- Date: Sat, 18 Aug 2001 00:49:00 +0100
Paul Skinner wrote on Fri, 17 Aug 2001 14:42:23 -0400:
>
on 8/17/01 12:51 PM, Victor Yee wrote:
>
> on addNumericSuffix(thisNum)
>
> set units to thisNum mod 10
>
> if units < 4 and thisNum mod 100 div 10 is not 1 then
>
> item (units + 1) of {"th", "st", "nd", "rd"}
>
> else
>
> "th"
>
> end if
>
> return {thisNum, result} as string
>
> end addNumericSuffix
>
>
>
.128 seconds/1000 vs. .26 for the original. Twice as fast! Both great
>
improvements.
>
>
Or I could've taken my own advice and thought out the logic a bit more
>
clearly. I completely believed I had, but nooo. So...
>
>
on ordinalSuffix(anInteger)
>
set lastPair to anInteger mod 100
>
set lastCharacter to lastPair mod 10
>
if lastCharacter < 4 and (lastPair < 10 or lastPair > 20) then
>
set suffix to item ((lastCharacter) + 1) of {"th", "st", "nd", "rd"}
>
else
>
set suffix to "th"
>
end if
>
return suffix
>
end ordinalSuffix
>
>
.095 seconds/1000
>
This should handle all instances of 'teen' endings.
That's cheating. :-) If you adjust your handler to return the number as
well as the suffix, you'll find that Victor's script is slightly faster.
NG