Re: Number -> String ( looking for code )

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

Re: Number -> String ( looking for code )

David Simmons
"Boris Popov" <[hidden email]> wrote in message
news:ku4j6.106244$[hidden email]...
> Well... It's not about "2 asString"... :)
> It's about 12 being converted to 'twelve'... And 101 being converted to
> 'hundred and one'... Etc etc etc
> I have had that code some time ago, but I can't find it at the moment. I
am
> pretty sure someone already has it... Could you share it please ? :)
>
> Dialect does not matter...
>
> Regards, Boris

Here is some code I grabbed out of SmalltalkAgents

You'll want to write the #divModPair:into: method.
--------------------------------------------------

method class=Integer [
asNumericString

    | pair remainder answer t1 t2 t3 t4 result newResult |

    pair := List new: 2.
    t1 := #(
        zero one two three four five six seven eight nine
        ten eleven twelve thirteen fourteen fifteen sixteen seventeen
        eighteen nineteen
    ).

    (self sign < 0) ifTrue:
    [
        answer := 'minus ' copy.
        result := self magnitude.
    ] ifFalse:
    [
        answer := String new.
        result := self.
    ].

    (result < 20) ifTrue:
    [
       ^answer, (t1 basicAt: result+1)
    ].

    t2 := #(twenty thirty fourty fifty sixty seventy eighty ninety).

    (result < 100) ifTrue:
    [
        result divModPair: 10 into: pair.
        remainder := pair at: 2.
        newResult := pair at: 1.

       ^remainder ifZero:
        [
            answer,(t2 at: newResult-1)
        ] ifNotZero:
        [
            answer,(t2 at: newResult-1),' ',(t1 basicAt: remainder+1)
        ]
    ].

    t3 := #(hundred thousand million billion trillion).

    t4 := #(100 1000 1000000 1000000000 1000000000000 1000000000000000).

    2 to: t4 basicSize do:
    [:index || range |

        range := t4 at: index.

        (result < range) ifTrue:
        [
            result divModPair: (t4 at: index-1) into: pair.
            remainder := pair at: 2.
            newResult := pair at: 1.

            remainder ifZero:
            [
               ^answer,
                    newResult asNumericString,' ',
                    (t3 at: index-1)
            ] ifNotZero:
            [
               ^answer,
                    newResult asNumericString,' ',
                    (t3 at: index-1),' ',
                    remainder asNumericString
            ].
        ].
    ].

    result divModPair: 1000000000000 into: pair.
    newResult := pair at: 1.

   ^(remainder := pair at: 2) ifZero:
    [
        answer,newResult asNumericString,' trillion'
    ] ifNotZero:
    [
        answer,newResult asNumericString,' trillion ',
            remainder asNumericString
    ]
].

-- Dave Simmons [www.qks.com / www.smallscript.com]
  "Effectively solving a problem begins with how you express it."