number to words continues - Bill need your help

# Thread: number to words continues - Bill need your help

1. Senior Member
Join Date
Dec 1969
Posts
468

## number to words continues - Bill need your help

I&#039;ve used the function you showed exactly the same way. It gives error "invalid use of NULL" and points to this line of the code.<BR> cents = Round( 100 * ( amount - dollars ) ) <BR><BR>Now I&#039;ve inserted the whole function in a file. included this file using &#060;!--include functionfile name --&#062;<BR><BR>Amount which need to be converted to words, comes from SQLserver database datatype is number. <BR>What could be the reason to get the above error?<BR><BR>&#060;%<BR>Dim UnitNames, TeenNames, DecadeNames<BR>UnitNames = Array(" zero"," one"," two"," three"," four", _<BR> " five"," six"," seven"," eight"," nine" )<BR>TeenNames = Array(" ten"," eleven"," twelve"," thirteen"," fourteen", _<BR> " fifteen"," sixteen"," seventeen"," eighteen"," nineteen" )<BR>DecadeNames = Array(" zero"," ten"," twenty"," thirty"," forty", _<BR> " fifty"," sixty"," seventy"," eighty"," ninety" )<BR><BR>Function NumberAsWord( num )<BR> Dim millions, thousands, hundreds, decades, result<BR><BR> If Not isNumeric( num ) Then<BR> NumberAsWord = "&#060;i&#062;That is NOT a valid number!&#060;/i&#062;"<BR> Exit Function<BR> End If<BR><BR> result = ""<BR> num = CDbl(num)<BR><BR> If num = 0 Then<BR> NumberAsWord = "Zero"<BR> Exit Function<BR> End If<BR><BR> If num &#060; 0 Then<BR> num = - num<BR> result = "&#060;i&#062;NEGATIVE&#060;/i&#062; "<BR> End If<BR><BR> millions = 999999<BR><BR> On Error Resume Next<BR> millions = num 1000000<BR> num = CLng( num MOD 1000000 )<BR> On Error GoTo 0<BR><BR> If millions &#062; 0 Then<BR> If millions &#062; 999 Then<BR> NumberAsWord = result & "BILLIONS and BILLIONS"<BR> Exit Function<BR> End If<BR> result = result & NumberAsWord( millions ) & " Million"<BR> If num = 0 Then<BR> NumberAsWord = result<BR> Exit Function<BR> End If<BR> End If<BR> <BR> thousands = num 1000<BR> num = num MOD 1000<BR> If thousands &#062; 0 Then<BR> result = result & NumberAsWord( thousands ) & " Thousand"<BR> If num = 0 Then<BR> NumberAsWord = result<BR> Exit Function<BR> End If<BR> End If<BR> <BR> hundreds = num 100<BR> num = num MOD 100 <BR> If hundreds &#062; 0 Then<BR> result = result & UnitNames( hundreds ) & " Hundred"<BR> End If<BR> <BR> decades = num 10<BR> num = num MOD 10<BR> If decades = 1 Then<BR> result = result & TeenNames( num ) <BR> Else<BR> If decades &#062; 1 Then<BR> result = result & DecadeNames( decades )<BR> End If<BR> If num &#062; 0 Then<BR> result = result & UnitNames( num )<BR> End If<BR> End If<BR> NumberAsWord = result<BR>End Function<BR><BR><BR>Function DollarsToWords( amount ) <BR> Dim dollars, cents, words <BR><BR> dollars = Int( amount ) <BR><BR> cents = Round( 100 * ( amount - dollars ) ) <BR><BR> words = NumberAsWord( dollars ) & " Dollars" <BR><BR> If cents = 0 Then <BR> words = words & " And Zero Cent" <BR> Elseif cents = 1 Then &#039; special case so no "s" on cent <BR> words = words & " And One Cent" <BR> Else <BR> words = words & " And " & NumberAsWord( cents ) & " Cents" <BR> End If <BR><BR> DollarsToWords = words <BR>End Function <BR>%&#062;

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Dunno what you did wrong

It worked perfectly for me.<BR><BR>I tried these test cases:<BR><BR>Response.Write DollarsToWords(32781.2781) & "&#060;P&#062;"<BR>Response.Write DollarsToWords(0) & "&#060;P&#062;"<BR>Response.Write DollarsToWords(22) & "&#060;P&#062;"<BR>Response.Write DollarsToWords(123456789.99) & "&#060;P&#062;"<BR><BR>You need to change "Zero Cent" to "Zero Cents" as that&#039;s proper English, and I don&#039;t like the way you chose to capitalize some words and not others. But the logic worked perfectly. <BR><BR>I would suggest that maybe you gave an invalid number to the function?<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
468

## RE: Dunno what you did wrong

Yes it works fine for me when I tested the script. Its just when I embed the code to work with database field then I get this error. Value of field in the table is interger (double). So that should not be a problem either.<BR><BR>I need to solve this using your Mantra "Debug Debug Debug"<BR>Yes I capitalized all words now. <BR><BR>Thanks for your help & time.<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Oh! If you do...

This:<BR> Response.Write DollarsToWords( RS("someField") )<BR><BR>That could cause problems, because you are actually passing an ADODB.Field *OBJECT* to the function!<BR><BR>You should try this:<BR> Response.Write DollarsToWords( RS("someField")[hl="yellow"].Value[/hl] )<BR><BR><BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
468

## RE: thanks!

I&#039;ll let you know what was wrong. Currently I do not have access to the server to debug pages.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•