Thanks a lot John for sharing this handler ; I added it in my docs, might be
usefull ;-))
Just a small modification:
Some cases were not processed as expected ; for example
212 returned "two hundred and ten two"
351 returned "three hundred and eleven"
So I added the case where char 2 of fld 1 = 1 and char 3 of fld 1 <> 0
At the beginning of the case 3 in order it be processed before the case char 2
of fld 1 > 0.
Thanks again
Best regards from Grenoble
André
Here is the script:
----------------------
on mouseUp
local singles,mixed,tens,charcount,theTextNumber,
----------------------
put ("one","two","three","four","five","six","seven","eight","nine") into
singles
put
("eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen")
into mixed
put
("ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")
into tens
put the number of chars of fld 1 into charcount
switch charcount
case 1
if char 1 of fld 1 = 0 then
put "Zero" into theTextNumber
else
put item (char charcount of fld 1) of singles into theTextNumber
end if
break
case 2
if char 1 of fld 1 = 1 and char 2 of fld 1 = 0 then
put "Ten" into theTextNumber
exit switch
end if
if char 1 of fld 1 = 1 and char 2 of fld 1 > 0 then
put item (char 2 of fld 1) of mixed into theTextNumber
exit switch
end if
if char 1 of fld 1 > 1 and char 2 of fld 1 = 0 then
put item (char 1 of fld 1) of tens into theTextNumber
exit switch
end if
if char 1 of fld 1 > 1 and char 2 of fld 1 > 0 then
put item (char 1 of fld 1) of tens && item (char 2 of fld 1) of
singles into theTextNumber
exit switch
end if
break
case 3
switch
case char 2 of fld 1 = 1
if char 3 of fld 1 <> 0 then
put item (char 1 of fld 1) of singles && "hundred and"
&& item (char 3 of fld 1) of mixed into theTextNumber
exit switch
end if
if char 3 of fld 1 = 0 then
put item (char 1 of fld 1) of singles && "hundred and"
&& item (char 2 of fld 1) of tens into theTextNumber
exit switch
end if
break
case char 2 of fld 1 = 0
if char 3 of fld 1 = 0 then
put item (char 1 of fld 1) of singles && "hundred" into
theTextNumber
exit switch
end if
if char 3 of fld 1 > 0 then
put item (char 1 of fld 1) of singles && "hundred and"
&& item (char 3 of fld 1) of singles into theTextNumber
exit switch
end if
break
case char 2 of fld 1 > 0
if char 3 of fld 1 = 0 then
put item (char 1 of fld 1) of singles && "hundred and"
&& item (char 2 of fld 1) of tens && item (char 3 of fld 1) of singles into
theTextNumber
exit switch
end if
if char 3 of fld 1 = 1 then
put item (char 1 of fld 1) of singles && "hundred and"
&& item (char 2 of fld 1) of tens && item (char 3 of fld 1) of singles into
theTextNumber
exit switch
end if
if char 3 of fld 1 > 1 then
put item (char 1 of fld 1) of singles && "hundred and"
&& item (char 2 of fld 1) of tens && item (char 3 of fld 1) of singles into
theTextNumber
exit switch
end if
if char 3 of fld 1 > 0 then
put item (char 1 of fld 1) of singles && "hundred and"
&& item (char 3 of fld 1) of mixed into theTextNumber
exit switch
end if
break
end switch
end switch
put theTextNumber into fld 2
end mouseUp
-----------------------
Le 9 déc. 2010 à 02:21, John Dixon a écrit :
>
> Oops... I made a mess of it the first time around... (blushing)
>
> on mouseUp
>
> put ("one","two","three","four","five","six","seven","eight","nine") into
> singles
> put
> ("eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen")
> into mixed
> put
> ("ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")
> into tens
>
> put the number of chars of fld 1 into charcount
>
> switch charcount
>
> case 1
> if char 1 of fld 1 = 0 then
> put "Zero" into theTextNumber
> else
> put item (char charcount of fld 1) of singles into theTextNumber
> end if
> break
>
> case 2
> if char 1 of fld 1 = 1 AND char 2 of fld 1 = 0 then
> put "Ten" into theTextNumber
> exit switch
> end if
>
> if char 1 of fld 1 = 1 AND char 2 of fld 1 > 0 then
> put item (char 2 of fld 1) of mixed into theTextNumber
> exit switch
> end if
>
> if char 1 of fld 1 > 1 AND char 2 of fld 1 = 0 then
> put item (char 1 of fld 1) of tens into theTextNumber
> exit switch
> end if
>
> if char 1 of fld 1 > 1 AND char 2 of fld 1 > 0 then
> put item (char 1 of fld 1) of tens && item (char 2 of fld 1) of
> singles into theTextNumber
> exit switch
> end if
> break
>
> case 3
> if char 2 of fld 1 = 0 AND char 3 of fld 1 = 0 then
> put item (char 1 of fld 1) of singles && "hundred" into
> theTextNumber
> exit switch
> end if
>
> if char 2 of fld 1 = 0 AND char 3 of fld 1 > 0 then
> put item (char 1 of fld 1) of singles && "hundred and" && item
> (char 3 of fld 1) of singles into theTextNumber
> exit switch
> end if
>
> if char 2 of fld 1 > 0 AND char 3 of fld 1 = 0 then
> put item (char 1 of fld 1) of singles && "hundred and" && item
> (char 2 of fld 1) of tens && item (char 3 of fld 1) of singles into
> theTextNumber
> exit switch
> end if
>
> if char 2 of fld 1 > 0 AND char 3 of fld 1 > 1 then
> put item (char 1 of fld 1) of singles && "hundred and" && item
> (char 2 of fld 1) of tens && item (char 3 of fld 1) of singles into
> theTextNumber
> exit switch
> end if
>
> if char 2 of fld 1 > 0 AND char 3 of fld 1 > 0 then
> put item (char 1 of fld 1) of singles && "hundred and" && item
> (char 3 of fld 1) of mixed into theTextNumber
> exit switch
> end if
>
> break
> end switch
>
> put theTextNumber into fld 2
> end mouseUp
>
> That should go up to 999...
>
>> Date: Wed, 8 Dec 2010 17:30:23 -0500
>> Subject: Number to text
>> From: [email protected]
>> To: [email protected]
>>
>> Hi any Idea on how to go about calculating numbers to text eg. 100, One
>> Hundred, ?
>> Thanks, Hershel
>> _______________________________________________
>> use-livecode mailing list
>> [email protected]
>> Please visit this url to subscribe, unsubscribe and manage your subscription
>> preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
> _______________________________________________
> use-livecode mailing list
> [email protected]
> Please visit this url to subscribe, unsubscribe and manage your subscription
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
_______________________________________________
use-livecode mailing list
[email protected]
Please visit this url to subscribe, unsubscribe and manage your subscription
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode