On 11-Dec-2008, at 1:52 PM, Lukas Renggli wrote:

Seaside 2.9 comes with the following method:

ordinalize: anInteger
        ^ (anInteger \\ 100 between: 11 and: 13)
                ifTrue: [ 'th' ]
                ifFalse: [
                        #('st' 'nd' 'rd')
                                at: anInteger \\ 10
                                ifAbsent: [ 'th' ] ]

To fit it nicely into Integer, and to make it work for negative numbers as well, then perhaps the following tiny adaptation of the Seaside method is better?


(-25 to: 25) collect: [:i | i asStringWithOrdinalSuffix] #('-25th' '-24th' '-23rd' '-22nd' '-21st' '-20th' '-19th' '-18th' '-17th' '-16th' '-15th' '-14th' '-13th' '-12th' '-11th' '-10th' '-9th' '-8th' '-7th' '-6th' '-5th' '-4th' '-3rd' '-2nd' '-1st' '0th' '1st' '2nd' '3rd' '4th' '5th' '6th' '7th' '8th' '9th' '10th' '11th' '12th' '13th' '14th' '15th' '16th' '17th' '18th' '19th' '20th' '21st' '22nd' '23rd' '24th' '25th')


'From Squeak3.10.2 of ''5 June 2008'' [latest update: #7179] on 11 December 2008 at 3:10:28 pm'!

!Integer methodsFor: 'printing' stamp: 'GAW 12/11/2008 15:04'!
asStringWithOrdinalSuffix
"Answer the string representation with English ordinal suffix concatenated"
        ^ self printString , self ordinalSuffix! !

!Integer methodsFor: 'printing' stamp: 'GAW 12/11/2008 15:03'!
ordinalSuffix
"Answer a string representing the English ordinal suffix for self"
        |abs|
        abs := self abs.
        ^ (abs \\ 100 between: 11 and: 13)
                ifTrue: ['th']
                ifFalse: [#('st' 'nd' 'rd' )
                                at: abs \\ 10
                                ifAbsent: ['th']]! !

--
                                        Greg A. Woods; Planix, Inc.
                                        <[email protected]>

Attachment: PGP.sig
Description: This is a digitally signed message part

_______________________________________________
Beginners mailing list
[email protected]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

Reply via email to