23 to 23rd?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

23 to 23rd?

Tim Johnson
Hi,

Does anyone have, or know of, existing code to perform the rather  
anglo-centric (?) function of turning 3 into '3rd,' 22 into '22nd,'  
etc.?

Thanks,
TimJ
 
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 23 to 23rd?

Yoshiki Ohshima-2
At Thu, 11 Dec 2008 12:23:45 -0600,
Tim Johnson wrote:
>
> Hi,
>
> Does anyone have, or know of, existing code to perform the rather  
> anglo-centric (?) function of turning 3 into '3rd,' 22 into '22nd,'  
> etc.?

  Heh, if Wikipedia is right, the following method for Integer would
do:

ordinal

        | str table lookup |
        str := self printString.
        "self <= 0 ifTrue: [...]."
        table := #('st' 'nd' 'rd').
        lookup := [:n | ^ str, (table at: n ifAbsent: ['th'])].
        self < 10 ifTrue: [lookup value: self].
        (str at: (str size - 1)) = $1 ifTrue: [^ str, 'th'].
        ^ lookup value: (self \\ 10).

(-10 to: 200) collect: [:i | i ordinal]

 #('-10th' '-9th' '-8th' '-7th' '-6th' '-5th' '-4th' '-3th' '-2th' '-1th' '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' '26th' '27th' '28th' '29th' '30th' '31st' '32nd' '33rd' '34th' '35th' '36th' '37th' '38th' '39th' '40th' '41st' '42nd' '43rd' '44th' '45th' '46th' '47th' '48th' '49th' '50th' '51st' '52nd' '53rd' '54th' '55th' '56th' '57th' '58th' '59th' '60th' '61st' '62nd' '63rd' '64th' '65th' '66th' '67th' '68th' '69th' '70th' '71st' '72nd' '73rd' '74th' '75th' '76th' '77th' '78th' '79th' '80th' '81st' '82nd' '83rd' '84th' '85th' '86th' '87th' '88th' '89th' '90th' '91st' '92nd' '93rd' '94th' '95th' '96th' '97th' '98th' '99th' '100th' '101st' '102nd' '103rd' '104th' '105th' '106th' '107th' '108th' '109th' '110th' '111th' '112th' '113th' '114th' '115th' '116th' '117th' '118th' '119th' '120th' '121st' '122nd' '123rd' '124th' '125th' '126th' '127th' '128th' '129th' '130th' '131st' '132nd' '133rd' '134th' '135th' '136th' '137th' '138th' '139th' '140th' '141st' '142nd' '143rd' '144th' '145th' '146th' '147th' '148th' '149th' '150th' '151st' '152nd' '153rd' '154th' '155th' '156th' '157th' '158th' '159th' '160th' '161st' '162nd' '163rd' '164th' '165th' '166th' '167th' '168th' '169th' '170th' '171st' '172nd' '173rd' '174th' '175th' '176th' '177th' '178th' '179th' '180th' '181st' '182nd' '183rd' '184th' '185th' '186th' '187th' '188th' '189th' '190th' '191st' '192nd' '193rd' '194th' '195th' '196th' '197th' '198th' '199th' '200th')


What would be the correct result for a negative number?

-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 23 to 23rd?

cedreek
In reply to this post by Tim Johnson
Hi,

I haven't found existing code. So I'll do that: in Integer

Integer>>englishEndString
        (self rem: 1) isZero ifTrue: [^'st'].
        (self rem: 2) isZero ifTrue: [^'nd'].
        (self rem: 3) isZero ifTrue: [^'rd'].
        ^'th'

Integer>>asEnglishString

        self asString , self englishEndString

hth,

2008/12/11 Tim Johnson <[hidden email]>:

> Hi,
>
> Does anyone have, or know of, existing code to perform the rather
> anglo-centric (?) function of turning 3 into '3rd,' 22 into '22nd,' etc.?
>
> Thanks,
> TimJ
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


--
Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 23 to 23rd?

Lukas Renggli
In reply to this post by Yoshiki Ohshima-2
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' ] ]

On Thu, Dec 11, 2008 at 7:48 PM, Yoshiki Ohshima <[hidden email]> wrote:

> At Thu, 11 Dec 2008 12:23:45 -0600,
> Tim Johnson wrote:
>>
>> Hi,
>>
>> Does anyone have, or know of, existing code to perform the rather
>> anglo-centric (?) function of turning 3 into '3rd,' 22 into '22nd,'
>> etc.?
>
>  Heh, if Wikipedia is right, the following method for Integer would
> do:
>
> ordinal
>
>        | str table lookup |
>        str := self printString.
>        "self <= 0 ifTrue: [...]."
>        table := #('st' 'nd' 'rd').
>        lookup := [:n | ^ str, (table at: n ifAbsent: ['th'])].
>        self < 10 ifTrue: [lookup value: self].
>        (str at: (str size - 1)) = $1 ifTrue: [^ str, 'th'].
>        ^ lookup value: (self \\ 10).
>
> (-10 to: 200) collect: [:i | i ordinal]
>
>  #('-10th' '-9th' '-8th' '-7th' '-6th' '-5th' '-4th' '-3th' '-2th' '-1th' '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' '26th' '27th' '28th' '29th' '30th' '31st' '32nd' '33rd' '34th' '35th' '36th' '37th' '38th' '39th' '40th' '41st' '42nd' '43rd' '44th' '45th' '46th' '47th' '48th' '49th' '50th' '51st' '52nd' '53rd' '54th' '55th' '56th' '57th' '58th' '59th' '60th' '61st' '62nd' '63rd' '64th' '65th' '66th' '67th' '68th' '69th' '70th' '71st' '72nd' '73rd' '74th' '75th' '76th' '77th' '78th' '79th' '80th' '81st' '82nd' '83rd' '84th' '85th' '86th' '87th' '88th' '89th' '90th' '91st' '92nd' '93rd' '94th' '95th' '96th' '97th' '98th' '99th' '100th' '101st' '102nd' '103rd' '104th' '105th' '106th' '107th' '108th' '109th' '110th' '111th' '112th' '113th' '114th' '115th' '116th' '117th' '118th' '119th' '120th' '121st' '122nd' '123rd' '124th' '125th' '126th' '127th' '128th' '129th' '130th' '131st' '132nd' '133rd' '134th' '135th' '136th' '137th' '138th' '139th' '140th' '141st' '142nd' '143rd' '144th' '145th' '146th' '147th' '148th' '149th' '150th' '151st' '152nd' '153rd' '154th' '155th' '156th' '157th' '158th' '159th' '160th' '161st' '162nd' '163rd' '164th' '165th' '166th' '167th' '168th' '169th' '170th' '171st' '172nd' '173rd' '174th' '175th' '176th' '177th' '178th' '179th' '180th' '181st' '182nd' '183rd' '184th' '185th' '186th' '187th' '188th' '189th' '190th' '191st' '192nd' '193rd' '194th' '195th' '196th' '197th' '198th' '199th' '200th')
>
>
> What would be the correct result for a negative number?
>
> -- Yoshiki
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>



--
Lukas Renggli
http://www.lukas-renggli.ch
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 23 to 23rd?

cedreek
In reply to this post by cedreek
oups too quick... doesn't work ;)

2008/12/11 Cédrick Béler <[hidden email]>:

> Hi,
>
> I haven't found existing code. So I'll do that: in Integer
>
> Integer>>englishEndString
>        (self rem: 1) isZero ifTrue: [^'st'].
>        (self rem: 2) isZero ifTrue: [^'nd'].
>        (self rem: 3) isZero ifTrue: [^'rd'].
>        ^'th'
>
> Integer>>asEnglishString
>
>        self asString , self englishEndString
>
> hth,
>
> 2008/12/11 Tim Johnson <[hidden email]>:
>> Hi,
>>
>> Does anyone have, or know of, existing code to perform the rather
>> anglo-centric (?) function of turning 3 into '3rd,' 22 into '22nd,' etc.?
>>
>> Thanks,
>> TimJ
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>
>
>
>
> --
> Cédrick
>


--
Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: 23 to 23rd?

Greg A. Woods; Planix, Inc.
In reply to this post by Lukas Renggli

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.
                                        <[hidden email]>


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

PGP.sig (193 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: 23 to 23rd?

Lukas Renggli
> 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?

Seaside 2.9 externalizes printing into external, composeable and
platform-independent printer objects. This allows Seaside to swap them
on the fly, for example depending on the locale of the current
session. The ordinal printer only generates the suffix. Printing the
sign and the number is the responsibility of other printer objects.

Cheers,
Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners