ashokkumar ss

ashokkumar ss

  • NA
  • 1
  • 1.6k

numtostring fucntion error - not getting string for more than 99999.

Jan 31 2012 2:00 AM
this is the code i wrote for convering the emp salary from db that should be displayed in string format in emp pay slip. it works for people who are having salary below 99999. but while go for more than 99999, it throws wrong value, for one tenlac, it should show one million, but it shows one hundred and soooo..

pls advice..

here is the function..

<%

 

Function NumToString(nNumber)

 

 

 

Dim bNegative

 

Dim bHundred

 

 

 

If nNumber < 0 Then

 

    bNegative = True

 

End If

 

 

 

nNumber = Abs(Int(nNumber))

 

 

 

If nNumber < 1000 Then

 

    If nNumber \ 100 > 0 Then

 

        NumToString = NumToString & _

 

             NumToString(nNumber \ 100) & " Hundred"

 

        bHundred = True

 

    End If

 

    nNumber = nNumber - ((nNumber \ 100) * 100)

 

    Dim bNoFirstDigit

 

    bNoFirstDigit = False

 

    Select Case nNumber \ 10

 

        Case 0

 

            Select Case nNumber Mod 10

 

                Case 0

 

                    If Not bHundred Then

 

                        NumToString = NumToString & " Zero"

 

                    End If

 

                Case 1: NumToString = NumToString & " One"

 

                Case 2: NumToString = NumToString & " Two"

 

                Case 3: NumToString = NumToString & " Three"

 

                Case 4: NumToString = NumToString & " Four"

 

                Case 5: NumToString = NumToString & " Five"

 

                Case 6: NumToString = NumToString & " Six"

 

                Case 7: NumToString = NumToString & " Seven"

 

                Case 8: NumToString = NumToString & " Eight"

 

                Case 9: NumToString = NumToString & " Nine"

 

            End Select

 

            bNoFirstDigit = True

 

        Case 1

 

            Select Case nNumber Mod 10

 

                Case 0: NumToString = NumToString & " Ten"

 

                Case 1: NumToString = NumToString & " Eleven"

 

                Case 2: NumToString = NumToString & " Twelve"

 

                Case 3: NumToString = NumToString & " Thirteen"

 

                Case 4: NumToString = NumToString & " Fourteen"

 

                Case 5: NumToString = NumToString & " Fifteen"

 

                Case 6: NumToString = NumToString & " Sixteen"

 

                Case 7: NumToString = NumToString & " Seventeen"

 

                Case 8: NumToString = NumToString & " Eighteen"

 

                Case 9: NumToString = NumToString & " Nineteen"

 

            End Select

 

            bNoFirstDigit = True

 

        Case 2: NumToString = NumToString & " Twenty"

 

        Case 3: NumToString = NumToString & " Thirty"

 

        Case 4: NumToString = NumToString & " Forty"

 

        Case 5: NumToString = NumToString & " Fifty"

 

        Case 6: NumToString = NumToString & " Sixty"

 

        Case 7: NumToString = NumToString & " Seventy"

 

        Case 8: NumToString = NumToString & " Eighty"

 

        Case 9: NumToString = NumToString & " Ninety"

 

    End Select

 

    If Not bNoFirstDigit Then

 

        If nNumber Mod 10 <> 0 Then

 

            NumToString = NumToString & "-" & _

 

                         Mid(NumToString(nNumber Mod 10), 2)

 

        End If

 

    End If

 

Else

 

    Dim nTemp

 

    nTemp = 10 ^ 12 'trillion

 

    Do While nTemp >= 1

 

        If nNumber >= nTemp Then

 

            NumToString = NumToString & _

 

                         NumToString(Int(nNumber / nTemp))

 

            Select Case Int(Log(nTemp) / Log(10) + 0.5)

 

                Case 12: NumToString = NumToString & " Trillion"

 

                Case 9: NumToString = NumToString & " Billion"

 

                Case 6: NumToString = NumToString & " Million"

 

                Case 3: NumToString = NumToString & " Thousand"

 

            End Select

 

 

 

            nNumber = nNumber - (Int(nNumber / nTemp) * nTemp)

 

        End If

 

        nTemp = nTemp / 1000

 

    Loop

 

End If

 

 

 

If bNegative Then

 

    NumToString = " negative" & NumToString

 

End If

 

 

 

End Function

 

 

 

Function DollarToString(nAmount)

 

    Dim nDollar

 

    Dim nCent

 

 

 

    nDollar = Int(nAmount)

 

    nCent = (Abs(nAmount) * 100) Mod 100

 

 

 

    DollarToString = NumToString(nDollar) & " Dollar"

 

 

 

    If Abs(nDollar) <> 1 Then

 

        DollarToString = DollarToString & "s"

 

    End If

 

 

 

    DollarToString = DollarToString & " and" & _

 

                     NumToString(nCent) & " cent"

 

 

 

    If Abs(nCent) <> 1 Then

 

        DollarToString = DollarToString & "s"

 

    End If

 

 

 

End Function

 

 

 

Response.Write NumToString(1258966)

 

%>