HyperCard: Script to Convert Dollar Amounts to Words (7/92)

Handler Name: dollarsToWords,numToStr,upperCase

This function converts a number of dollars into words, just as you do when
writing a check. Try this: answer dollarsToWords(1234567.89)

function dollarsToWords num
get offset(".",num) -- any cents?
if it <> 0 then put return into char it of num
put line 1 of num into dollars
put line 2 of num into cents
put numToStr(dollars) into result
put upperCase(char 1 of result) into char 1 of result
if cents > 0 then put " and " & cents & "/100" after result
return result
end dollarsToWords

function numToStr num
put empty into result

-- trillions, billions, millions, thousands
repeat with i = 1 to 4
put length(num) - (15-3*i) into lastDigit
if lastDigit <= 0 then next repeat
put char 1 to lastDigit of num into thisNum
delete char 1 to lastDigit of num
if thisNum = 0 then next repeat
put numToStr(thisNum) after result
put item i of " trillion, billion, million, thousand" after result
if num = 0 then return result
put ", " after result
end repeat

-- hundreds
if num > 99 then
put numToStr(char 1 of num) & " hundred" after result
delete char 1 of num
if num = 0 then return result
put " " after result
else if char 1 of num is 0 then delete char 1 of num

put "twen,thir,four,fif,six,seven,eigh,nine" into prefixes

-- twenty..ninety
if num > 19 then
put item (char 1 of num) - 1 of prefixes & "ty" after result
delete char 1 of num
if num = 0 then return result
put "-" after result
end if

-- thirteen..nineteen
if num > 12 then
put item num - 11 of prefixes & "teen" after result
return result
end if

-- zero..twelve
return result & item num + 1 of ┬
"zero,one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve"
end numToStr

function upperCase ch
get charToNum(ch)
if it < 97 then return ch
else return numToChar(it - 32)
end upperCase


This article is adapted from the Claris Tech Info database.
Published Date: Feb 19, 2012