Number To Word

selvaganapathy 0 Tallied Votes 303 Views Share

This code snippet convert Number to String in VB6. For instance input 100 will be return as Hundred, It converts upto Core....

Option Explicit
Public Function UnitString(ByVal Ind As Integer) As String
   Dim ToStr As Variant
   ToStr = Array("Zero", "One", "Two", "Three", "Four", "Five", _
                                                "Six", "Seven", "Eight", "Nine", "Ten", _
                                                "Eleven", "Tweleve", "Thirteen", "Fourteen", "Fifteen", _
                                                "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty")
   UnitString = ToStr(Ind)
End Function
Public Function TenString(ByVal Ind As Integer) As String
   Dim ToStr As Variant
   ToStr = Array("Ten", "Twenty", "Thirty", "Fourty", "Fifty", _
                 "Sixty", "Seventy", "Eighty", "Ninety" _
                 )
   TenString = ToStr(Ind)
End Function
Public Function PlaceString(ByVal Ind As Integer) As String
   Dim ToStr As Variant
   ToStr = Array("Unit", "Tenth", "Hundred", _
                  "Thousand", "Laksh", "Crore", _
                  "Hundred" _
                 )
   PlaceString = ToStr(Ind)

End Function

Public Function ToString(ByVal No As Long) As String
   Dim ToStr As String
   'Dim No As Long
   Dim Value As Long
   Dim Divisor As Long
   Dim i As Long
   
   Divisor = 1000000000
   
   '  Crore TenLak Lak TenThous | Thous | Hund Ten Unit
'   If No > Divisor Then
'      Dim Remin As Long
'      Remin = Int(No / Divisor)
'      ToStr = ToString(Remin)
'   End If
   
   
   i = 6
   Do While (No >= 1000)
      Value = Int(No / Divisor)
      No = No Mod Divisor
      Divisor = Int(Divisor / 100)
      If (Value <> 0) Then
         ToStr = ToStr & NumberToString100(Value) & " " & PlaceString(i) + " "
      End If
      i = i - 1
   Loop
      
   Value = Int(No / 100)
   No = No Mod 100
   
   If (Value <> 0) Then
      ToStr = ToStr & NumberToString100(Value) & " " & PlaceString(i) + " "
   End If
   i = i - 1
   
   If No <> 0 Then
      ToStr = ToStr & NumberToString100(No)
   End If
   ToString = Trim(ToStr)
   End Function
Private Function NumberToString100(ByVal No As Integer) As String
   Dim ToStr As String
   Dim Ten As Integer, Unit As Integer
   
   If (No <= 20) Then
      NumberToString100 = UnitString(No)
      Exit Function
   End If
   No = No Mod 100
   Ten = Int(No / 10)
   No = No Mod 10
   Unit = No
   If (Ten >= 2) Then
      ToStr = ToStr & " " & TenString(Ten - 1)
      If (Unit > 0) Then
         ToStr = ToStr & " " & UnitString(Unit)
      End If
   ElseIf (Ten > 0) Then
      ToStr = ToStr & " " & UnitString((Ten * 10) + Unit)
   ElseIf (Unit > 0) Then
      ToStr = ToStr & " " & UnitString(Unit)
   End If
   
   NumberToString100 = Trim(ToStr)
End Function
Usage and Example:
 In a Form
 private sub Form_Load()
     MsgBox ToString(10023)
 End Sub
skinny_1990 0 Newbie Poster

i hope u can teach me the very basic codes of visual basic because it is our subject this coming opening of school...

i hope u can help me with my homeworks...

thnx...

marivic

a.j. 0 Newbie Poster

i dont know if u mind...but i can teach you what i know, just ask me.(i recently took a course in VB so....)
<i> just ask me, the name is aj</i>

cyberzeph 0 Newbie Poster

tanx a lot i learn from this

legs067 0 Newbie Poster

hi.. can i ask something...


hmmm can u pls convert your number to word ..from vb to .net.. hmmm tnx.. using if else structure??

chal 0 Newbie Poster

plsss give me some examples of database connect to visualbasic. . .about the sales or inventory that have a simple code. . plsssssss because this is my project. . . plssss help me

Keitselepe 0 Newbie Poster

So on start up form I have made introduced a button that call up another form in my project (visual basic2008) and the code are dim f as new form
f.show form2
So what I want to do now is to draw a shape in form 2 ,and the parameters of shape in form 2 should be entered on start form.

SoftwarePaladin 0 Newbie Poster

thanks for the code man it was very helpful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.