Number To Word

selvaganapathy 0 Tallied Votes 324 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

Dani AI

Generated

provided a useful VB6 routine that turns integers into words (Indian grouping: thousand, lakh, crore). The implementation is a good starting point, but there are a few practical issues and edge cases to fix before using it in production.

Key issues to address

  • Zero and negatives: the routine can return an empty string for 0 and does not handle negative numbers. Add an early check to return "Zero" and optionally prefix "Minus" for negatives.
  • Spelling and naming: correct typos (for example "Tweleve" -> "Twelve", "Fourty" -> "Forty", "Laksh" -> "Lakh") and remove duplicate/misplaced place names.
  • Indexing and grouping: array indexing (Unit/Ten arrays and how they are referenced) and the PlaceString-to-group mapping are brittle — verify each array index maps to the exact numeric group produced by your divisor logic. Off‑by‑one indexing is a common source of wrong words.
  • Numeric limits: VB6 Long is 32‑bit signed; numbers above 2,147,483,647 will overflow. For larger values use string-based grouping or larger numeric types when porting.
  • Formatting/whitespace: keep Trim around the final result and avoid building strings with extra leading spaces.

Porting & enhancements (for and others)

  • VB.NET: prefer Int64 (Long) or System.Numerics.BigInteger for arbitrary precision. Use StringBuilder for efficient concatenation.
  • Internationalization: make grouping configurable (Indian: 3-2-2 vs Western: 3-3-3) and supply two place-name sets (thousand/million/billion vs thousand/lakh/crore).
  • Fractions/currency: split integer and fractional parts; convert cents as "and XX/100" or say digits after the decimal as "point four five".
  • Testing: add unit tests for 0, single digits, teens, tens, 99, 100, 101, 999, 1000, 1001, 10000, 100000, 1000000, and the boundary 2147483647.

Quick checklist to make this thread code robust

  1. return "Zero" for 0 and handle negatives, 2) fix spelling/place-name typos, 3) verify array indexes against grouping logic, 4) add unit tests across the ranges above, 5) document whether output uses Indian or Western grouping.

Thanks to for the original code and to and others for confirming it was helpful — these fixes will make it reliable for real use.

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.