Number names?
R Allan Baruz
abaruz at rttsweb.com
Mon Aug 27 04:18:52 UTC 2001
Ned,
I was going to send this, then saw Robert Jarvis's implementation, but then
decided, what the hell? I worked on it for a little while when I was stuck
on client site in the wintertime, and haven't looked at it since, so use
with caution. If I had been more proficient at Smalltalk I probably would
have handled the special cases similarly to Robert's implementation.
Cheers,
R Allan Baruz
Enclosure
> From: Ned Konz <ned at bike-nomad.com>
> Reply-To: squeak-dev at lists.squeakfoundation.org
> Date: Fri, 24 Aug 2001 11:01:50 -0700
> To: squeak-dev at lists.squeakfoundation.org
> Subject: Re: Number names?
>
> On Friday 24 August 2001 10:05 am, Dan Ingalls wrote:
>
> Does anyone have or know of, in Squeak or any other Smalltalk...
>
> a method that if you give it
>
> SmallInteger maxVal
>
> it will respond with...
>
> 'one billion, seventy-three million, seven hundred forty-one
> thousand, eight hundred twenty-three'
>
> As opposed to, say, 'one thousand seventy-three million seven hundred
> forty-one thousand, eight hundred twenty-three'?
>
> Boris Popov did one in VW that you can get via the Cincom Wiki:
> http://www.cincomsmalltalk.com:8080/CincomSmalltalkWiki/DIFF/6/5/Number+To+Num
> eric+String+Convertor
>
> --
> Ned Konz
> currently: Stanwood, WA
> email: ned at bike-nomad.com
> homepage: http://bike-nomad.com
>
-------------- next part --------------
'From Squeak2.9alpha of 13 June 2000 [latest update: #3077] on 21 January 2001 at 3:30:51 pm'!
"Change Set: IntegerWords
Date: 26 January 2001
Author: R Allan Baruz
In good usage the first twenty numbers (sometimes the first hundred)
are written out. Raskin's Canon Cat can turn this:
Joe found 5-2 apples
into:
Joe found 3 apples
with one selection and one keystroke. This looks wrong to me. Rather:
Joe found three apples.
Anyway, I got carried away. Also useful for writing checks."!
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 01:35'!
asCohortCollection
| rs ws |
rs _ ReadStream on: self abs printString reverse.
ws _ WriteStream on: #().
[rs atEnd] whileFalse: [ws nextPut: (rs next: 3) reverse asNumber].
^ws contents reverse! !
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 01:36'!
asCohortWord
"Given a three-digit cohort, return it as a word."
| hundreds tensAndOnes ones strm |
strm _ WriteStream on: ''.
hundreds _ self // 100.
tensAndOnes _ self \\ 100.
hundreds = 0
ifFalse: [strm nextPutAll: hundreds asWordSpecialCase , ' hundred'].
tensAndOnes = 0
ifFalse: [strm nextPut: $ .
tensAndOnes < 20
ifTrue: [strm nextPutAll: tensAndOnes asWordSpecialCase]
ifFalse: [strm nextPutAll: (tensAndOnes // 10 * 10) asWordSpecialCase.
ones _( tensAndOnes \\ 10).
(ones = 0) ifFalse: [strm nextPutAll: '-', ones asWordSpecialCase]]].
^ strm contents withBlanksTrimmed! !
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 02:30'!
asOrdinalSpecialCase
"Ugly!! but what do I do?"
^ self caseOf: {
[0] -> [''].
[1] -> ['first'].
[2] -> ['second'].
[3] -> ['third'].
[4] -> ['fourth'].
[5] -> ['fifth'].
[6] -> ['sixth'].
[7] -> ['seventh'].
[8] -> ['eighth'].
[9] -> ['ninth'].
[10] -> ['tenth'].
[11] -> ['eleventh'].
[12] -> ['twelfth'].
[13] -> ['thirteenth'].
[14] -> ['fourteenth'].
[15] -> ['fifteenth'].
[16] -> ['sixteenth'].
[17] -> ['seventeenth'].
[18] -> ['eighteenth'].
[19] -> ['nineteenth'].
[20] -> ['twentieth'].
[30] -> ['thirtieth'].
[40] -> ['fortieth'].
[50] -> ['fiftieth'].
[60] -> ['sixtieth'].
[70] -> ['seventieth'].
[80] -> ['eightieth'].
[90] -> ['ninetieth']}! !
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 01:18'!
asStringWithCommas
"123456789 asStringWithCommas"
"-123456789 asStringWithCommas"
| digits |
digits _ self abs printString.
^ String streamContents:
[:strm |
self sign = -1 ifTrue: [strm nextPut: $-].
1 to: digits size do:
[:i | strm nextPut: (digits at: i).
(i < digits size and: [(i - digits size) \\ 3 = 0])
ifTrue: [strm nextPut: $,]]]! !
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 10:17'!
asStringWithCommasYuk
"123456789 asStringWithCommasYuk"
"-123456789 asStringWithCommasYuk Whoops!!"
"10000005 asStringWithCommasYuk Whoops!!"
^ (self asCohortCollection
inject: ''
into: [:string :i | string , i asThreeCharacterString , ',']) allButLast! !
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 10:15'!
asThreeCharacterString
"Answer a three-character string representing the receiver, with leading zeros if required. Intended for use with integers in the range 0 to 999"
^ self printString padded: #left to: 3 with: $0
"
2 asThreeCharacterString
11 asThreeCharacterString
1943 asThreeCharacterString
0 asThreeCharacterString
-2 asThreeCharacterString Whoops!!
-234 asThreeCharacterString
"! !
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 01:32'!
asWord
"In good usage the first twenty numbers (sometimes the first hundred)
are written out. Raskin's Canon Cat can turn this:
Joe found 5-2 apples
into:
Joe found 3 apples
with one selection and one keystroke. This looks wrong to me. Rather:
Joe found three apples.
Anyway, I got carried away. Also useful for writing checks."
"123456789 asWord"
| cohortStream wordStream cohorts commas |
self = 0 ifTrue: [^ 'zero'].
wordStream _ WriteStream on: ''.
cohorts _ self asCohortCollection.
cohortStream _ ReadStream on: self asCohortCollection.
commas _ cohorts size - 1.
self sign = -1
ifTrue: [wordStream nextPutAll: 'negative '].
[cohortStream atEnd]
whileFalse:
[wordStream
nextPutAll: cohortStream next asCohortWord;
nextPutAll: ' ' , commas cohortIdentifier , ' '.
commas _ commas - 1].
^ wordStream contents withBlanksTrimmed.
"cohorts _ self asCohortCollection.
commas _ cohorts size.
^cohorts inject '' into: [ :string :cohort | string _ string, ' ', cohort asCohortWord]"
! !
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 01:32'!
asWordSpecialCase
"Ugly!! but what do I do?"
^ self caseOf: {
[0] -> [''].
[1] -> ['one'].
[2] -> ['two'].
[3] -> ['three'].
[4] -> ['four'].
[5] -> ['five'].
[6] -> ['six'].
[7] -> ['seven'].
[8] -> ['eight'].
[9] -> ['nine'].
[10] -> ['ten'].
[11] -> ['eleven'].
[12] -> ['twelve'].
[13] -> ['thirteen'].
[14] -> ['fourteen'].
[15] -> ['fifteen'].
[16] -> ['sixteen'].
[17] -> ['seventeen'].
[18] -> ['eighteen'].
[19] -> ['nineteen'].
[20] -> ['twenty'].
[30] -> ['thirty'].
[40] -> ['forty'].
[50] -> ['fifty'].
[60] -> ['sixty'].
[70] -> ['seventy'].
[80] -> ['eighty'].
[90] -> ['ninety']}! !
!Integer methodsFor: 'printing' stamp: 'RAB 1/20/2001 09:24'!
cohortIdentifier
"Yuck!! Oh, well.
I think the Anglo-German usage may differ from the Franco-American."
^ self caseOf: {
[0] -> [''].
[1] -> ['thousand'].
[2] -> ['million'].
[3] -> ['billion'].
[4] -> ['trillion'].
"[5] -> ['quadrillion'].
[6] -> ['quintillion']"}
otherwise:
[6 atRandom caseOf: {
[1] -> ['bazillion'].
[2] -> ['mumblety-ion'].
[3] -> ['gazillion'].
[4] -> ['*cough*illion'].
[5] -> ['bajillion'].
[6] -> ['zillion'].}]
"If you must go beyond the trillions, you should probably be using
scientific notation. Or an accountant."! !
More information about the Squeak-dev
mailing list
|