Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[VB6]Number to Text conversion

This function converts number to text in the range 0.00 to 999999999.99

Declarations:
Code
'none

Code:
Code
'To a form add one text box (Text1) and one label (Label1) and one command button (command1).
'Copy the following form code and past in the form. Just run....

Private Sub Command1_Click()
    Label1.Caption = Number2Word(Val(Text1.Text))
End Sub

Private Function Number2Word(ByVal pNum As Double) As String

On Error GoTo ErrHdl
Dim ar1(19) As String
Dim ar2(10) As String
Dim ar3(5) As String
ar1(0) = ""
ar1(1) = "One"
ar1(2) = "Two"
ar1(3) = "Three"
ar1(4) = "Four"
ar1(5) = "Five"
ar1(6) = "Six"
ar1(7) = "Seven"
ar1(8) = "Eight"
ar1(9) = "Nine"
ar1(10) = "Ten"
ar1(11) = "Eleven"
ar1(12) = "Twelve"
ar1(13) = "Thirteen"
ar1(14) = "Fourteen"
ar1(15) = "Fifteen"
ar1(16) = "Sixteen"
ar1(17) = "Seventeen"
ar1(18) = "Eighteen"
ar1(19) = "Ninteen"
ar2(1) = "Ten"
ar2(2) = "Twenty"
ar2(3) = "Thirty"
ar2(4) = "Fourty"
ar2(5) = "Fifty"
ar2(6) = "Sixty"
ar2(7) = "Seventy"
ar2(8) = "Eighty"
ar2(9) = "Ninty"
ar3(0) = ""
ar3(1) = "Hundred"
ar3(2) = "Thousand"
ar3(3) = "Lakh"
ar3(4) = "Crore"


Dim tmpNum As Double
Dim tmpInt As Long
Dim tmpDml As Integer
Dim strWord As String
Dim strWordpart As String
Dim tmppart As Integer
Dim lcount As Integer

tmpNum = pNum
tmpInt = Int(tmpNum)
tmpDml = (tmpNum - tmpInt) * 100

'Integer conversion
If tmpInt < 0 Or tmpInt > 999999999 Then
    MsgBox "Convertible range is 0.00 to 999999999.99 only" & vbCrLf & "Sorry for the inconvenience..."
    Exit Function
End If
If tmpInt = 0 Then
    strWord = "Zero"
ElseIf tmpInt < 20 Then
    strWord = ar1(tmpInt)
Else
    lcount = -1
    Do While tmpInt > 0
        strWordpart = ""
        If lcount < 0 Or lcount > 0 Then
            tmppart = Right(Trim(Str(tmpInt)), 2)
            If Val(tmpInt) >= 10 Then
                tmpInt = Val(Left(Trim(Str(tmpInt)), Len(Trim(Str(tmpInt))) - 2))
            Else
                tmpInt = Val(Left(Trim(Str(tmpInt)), Len(Trim(Str(tmpInt))) - 1))
            End If
        ElseIf lcount = 0 Then
            tmppart = Right(Trim(Str(tmpInt)), 1)
            tmpInt = Val(Left(Trim(Str(tmpInt)), Len(Trim(Str(tmpInt))) - 1))
        End If
        lcount = lcount + 1
        If tmppart < 20 Then
            strWordpart = ar1(tmppart)
        Else
            strWordpart = ar2(Int(tmppart / 10)) & " " & ar1(tmppart Mod 10)
        End If
        If Not Trim(strWordpart) = "" Then
            strWord = strWordpart & " " & ar3(lcount) & " " & strWord
        End If
    Loop
End If
strWord = "Rs. " & strWord

' Decimal part conversion
' This will be added to string only if paise part exists.
strWordpart = ""
If tmpDml < 20 Then
    strWordpart = ar1(tmpDml)
Else
    strWordpart = ar2(Int(tmpDml / 10)) & " " & ar1(tmpDml Mod 10)
End If
If Not Trim(strWordpart) = "" Then
    strWord = Trim(strWord) & " and " & strWordpart & " paise Only"
Else
    strWord = Trim(strWord) & " Only"
End If

' Retun final output
Number2Word = strWord
Exit Function

ErrHdl:
    MsgBox "value out of scope"
    Number2Word = ""
End Function

orignally written by:Dinesh G

If You Like this tutorial,
please check out my other tutorials by clicking ==> here
PHP & .NET Contributor.
BUMP!

If You Like this tutorial,
please check out my other tutorials by clicking ==> here
PHP & .NET Contributor.
Great share mate.. ✌
thanks for the feedback...

:D
PHP & .NET Contributor.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by debugDec 22, 2020 04:51 PM 0 228 Dec 22, 2020 04:51 PM
Last Post: debug
[Python] by Fivepoint0Oct 23, 2018 05:24 PM 3 191 Oct 23, 2018 05:24 PM
Last Post: Fivepoint0
[.NET] by ToxicLexxiMar 12, 2018 06:22 AM 1 180 Mar 12, 2018 06:22 AM
Last Post: HFDone