Thursday, April 17, 2008

Convert Number to Word

In my first job, I needed to have a function to verify the number to string conversion. The number should be converted to the word. For example, if user enters 8908 and function returns the output as Eight Thousand Nine Hundred and Eight only. I thought that it is very simple. After starting to write it, I faced few issues. Thats why I created four functions. I have given the code below. The current code converts up to crores. This code snippet can be used in Visual Test or Visual Basic. Few changes needs to be done to use in VB Script.

Code: Returns the word to the given number



'---------------------------
' Method : NumberToWord
' Author : T.Palani Selvam
' Purpose : Returns the word to the given number.
' Parameters: sGivenNum - String, contains to be converted number.
'
' Returns : Returns String of corresponding number.
' Caller :
' Calls : - Nil -
'----------------------------
Function NumberToWord(sGivenNum As String) As String
Dim lBeforeDecimal As Long, lAfterDecimal As Long
Dim iDecimalPos As Integer, iIndex As Integer
Dim sBeforeDecimalWord As String, sAfterDecimalWord As String
Dim sResult As String, sChar As String

'sResult = Null
sGivenNum = Trim(Str(Val(sGivenNum)))
If Val(sGivenNum) = 0 Then
NumberToWord = "Zero"
Exit Function
End If
sAfterDecimalWord = ""
sBeforeDecimalWord = ""
iDecimalPos = InStr(sGivenNum, ".")
If iDecimalPos = 0 Then
lBeforeDecimal = Val(sGivenNum)
Else
'sGivenNum = Format(sGivenNum, "#####.00")
lBeforeDecimal = Val(Left(sGivenNum, iDecimalPos - 1))
lAfterDecimal = Val(Mid(sGivenNum, iDecimalPos + 1))
If (lAfterDecimal <> 0) Then
'sAfterDecimalWord = " Paise " + PrimaryConversion(lAfterDecimal)
sAfterDecimalWord = " Point" '+ PrimaryConversion(lAfterDecimal)
For iIndex = 1 To Len(trim(str(lAfterDecimal)))
sChar = Mid$(trim(str(lAfterDecimal)), iIndex, 1)
sAfterDecimalWord = sAfterDecimalWord + " " + PrimaryConversion(val(sChar))
Next iIndex
End IF
If (lBeforeDecimal = 0) Then
NumberToWord = sAfterDecimalWord
Exit Function
End IF
End If

sBeforeDecimalWord = SplitType(10000000, " Crore ", lBeforeDecimal)
sBeforeDecimalWord = sBeforeDecimalWord + SplitType(100000, " Lakh ", lBeforeDecimal)
sBeforeDecimalWord = sBeforeDecimalWord + SplitType(1000, " Thousand ", lBeforeDecimal)
sBeforeDecimalWord = sBeforeDecimalWord + SplitType(100, " Hundred ", lBeforeDecimal)
If sBeforeDecimalWord = "" Then
sBeforeDecimalWord = PrimaryConversion(lBeforeDecimal)
Else
sBeforeDecimalWord = sBeforeDecimalWord + "and " + PrimaryConversion(lBeforeDecimal)
End If
sResult = sBeforeDecimalWord

If sAfterDecimalWord = "" Then
sResult = sBeforeDecimalWord
Else
'sResult = sBeforeDecimalWord + " and" + sAfterDecimalWord
sResult = sBeforeDecimalWord + sAfterDecimalWord
End If

NumberToWord = sResult '+ " Only"
End Function


Code: Set the word according to level


'-------------------------
' Method : SplitType
' Author : T.Palani Selvam
' Purpose : Splits a level to next level.
' Parameters: lNumType - Long, contains like 100, 1000
' sWord - String, contains before conversioned word.
' Returns : Returns String of corresponding number.
'----------------------------
Function SplitType(lNumType As Long, sWord As String,lBeforeDecimal As long ) As String
'Dim lBeforeDecimal As Long
Dim lNumberType As Long

SplitType = ""
lNumberType = lBeforeDecimal \ lNumType
If lNumberType > 0 Then
lBeforeDecimal = lBeforeDecimal - lNumberType * lNumType
SplitType = PrimaryConversion(lNumberType) + sWord
End If
End Function


Code: Parse a string to the given condition


'--------------------------
' Method : GetParsedString
' Author : T.Palani Selvam
' Purpose : Parse a string to the given condition
' Parameters: sData - String, contains full string..
' sSeparate - String, contains separation string
' iOccur - String, contains position of the Split
' Returns : Returns String, Null or other values.
'--------------------------
Function GetParsedString(sData As String, sSeparate As String, iOccur As Integer) As String
Dim iPosition As Integer, iIndex As Integer
Dim sTemp As String

GetParsedString = Null
sTemp = sData

For iIndex =1 To (iOccur -1) Step 1
iPosition = Instr(sTemp,sSeparate)
If (iPosition > 0) Then
sTemp=Mid$(sTemp,len(sSeparate) + iPosition)
Else
Print(sSeparate + " is not occured")
Exit Function
End If
' Print "Original Symbol: " + sTemp, sSeparate
Next iIndex
iPosition = Instr(sTemp,sSeparate)
If (iPosition > 0) Then
sTemp=Left$(sTemp,iPosition - 1)
End If
GetParsedString = sTemp
End Function


Code: Converts primary number to word


'----------------------
' Method : PrimaryConversion
' Author : T.Palani Selvam
' Purpose : It converts primary number to string.
' Parameters: lNumber - Long, contains number
'
' Returns : Returns String of corresponding number.
'-------------------------
Function PrimaryConversion(lNumber As Long) As String
PrimaryConversion = ""
Select Case lNumber
Case 0
Case 1
PrimaryConversion = "One"
Case 2
PrimaryConversion = "Two"
Case 3
PrimaryConversion = "Three"
Case 4
PrimaryConversion = "Four"
Case 5
PrimaryConversion = "Five"
Case 6
PrimaryConversion = "Six"
Case 7
PrimaryConversion = "Seven"
Case 8
PrimaryConversion = "Eight"
Case 9
PrimaryConversion = "Nine"
Case 10
PrimaryConversion = "Ten"
Case 11
PrimaryConversion = "Eleven"
Case 12
PrimaryConversion = "Twelve"
Case 13
PrimaryConversion = "Thirteen"
Case 14
PrimaryConversion = "Fourteen"
Case 15
PrimaryConversion = "Fifteen"
Case 16
PrimaryConversion = "Sixteen"
Case 17
PrimaryConversion = "Seventeen"
Case 18
PrimaryConversion = "Eighteen"
Case 19
PrimaryConversion = "Nineteen"
Case 20
PrimaryConversion = "Twenty"
Case 30
PrimaryConversion = "Thirty"
Case 40
PrimaryConversion = "Forty"
Case 50
PrimaryConversion = "Fifty"
Case 60
PrimaryConversion = "Sixty"
Case 70
PrimaryConversion = "Seventy"
Case 80
PrimaryConversion = "Eighty"
Case 90
PrimaryConversion = "Ninety"
Case Else
PrimaryConversion = PrimaryConversion(lNumber - (lNumber Mod 10)) + " " + PrimaryConversion(lNumber Mod 10)
End Select
End Function

1 comment:

Unknown said...

I found this useful. Thanks.