This code snippet convert Number to String in VB6. For instance input 100 will be return as Hundred, It converts upto Core....
Number To Word
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
a.j. 0 Newbie Poster
cyberzeph 0 Newbie Poster
legs067 0 Newbie Poster
chal 0 Newbie Poster
Keitselepe 0 Newbie Poster
SoftwarePaladin 0 Newbie Poster
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.