Showing posts with label Visualbasic. Show all posts
Showing posts with label Visualbasic. Show all posts

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

Tuesday, March 18, 2008

MS XML Parser by Visual Basic

I love to use XML Parser. It is very easy to do by Visual basic. You can get the equivalent visual test code here - Visual Test - MS XML Parser. I'm thinking to use it in Quick Test Professional (QTP). Below code snippets are simple and easy to use.

Case1: To load any XML content



'-----------------------------------------
' Method : XMLParserLoadXML
' Author : T.Palani Selvam
' Purpose : Creates XML Parsing object according to the given XML String.
' Parameters: sXML - String, contains data in XML Format.
' Returns : Returns a Object, which contains XML Parsing object.
' Caller : - Nil
' Calls : - Nil -
'------------------------------------------
Function XMLParserLoadXML(sXML As String) As Variant
Dim objXML As Variant

XMLParserLoadXML = Null

Set objXML = CreateObject("Microsoft.XMLDOM") 'Declare a Object
objXML.async = False
objXML.loadXML (sXML) 'Load xmlformat String

'OleSetProperty(objXML,"preserveWhiteSpace") = False

If (objXML.parseError <> 0) Then
LogWrite ("Parsing Error in the File :" + sXMLFile)
Exit Function
End If

'Top Node of given file
Set XMLParserLoadXML = objXML.documentElement
End Function



Case2: It parsed the given tag in given content and returns the node.


'-----------------------------------------
' Method : XMLParser
' Author : T.Palani Selvam
' Purpose : Creates XML Parsing object according to the given XML Format File and Element.
' Parameters: sXMLFile - String, contains a file name, which is in XML Format.
' sTag - String, contains element name from which Tag or element based object to be returned.
' Returns : Returns a Object, which contains XML Parsing object.
' Caller : - Nil
' Calls : - Nil -
'-----------------------------------------
Function XMLParser(sXMLFile As String, sTag As String) As Variant
Dim objXML As Variant, objNode As Variant, objTemp As Variant, objRoot As Variant
'Dim varTest As Variant
Dim iIndex As Integer, iTemp As Integer
'Dim objRoot As node

XMLParser = Null
sTag = Trim(sTag)
Set objXML = CreateObject("Microsoft.XMLDOM") 'Declare a Object
objXML.async = False
objXML.Load (sXMLFile) 'Load xmlformat file

If (objXML.parseError <> 0) Then
LogWrite ("Parsing Error in the File :" + sXMLFile)
MsgBox ("Parsing Error in the File :" + sXMLFile)
Exit Function
End If

'Top Node of given file
Set objRoot = objXML.documentElement
'Print "XML File is : " & sXMLFile
'Print objRoot.nodeName & " Name of the node.."

If ((sTag = "") Or (sTag = objRoot.nodeName)) Then
'XMLParser = CVar(objRoot)
Set XMLParser = objRoot
'Set varTest = objRoot
'XMLParser = objRoot
Exit Function
End If

If objRoot.hasChildNodes <> 0 Then
objTemp = objRoot.childNodes
iTemp = objTemp.length

For iIndex = 0 To iTemp - 1
objNode = objTemp.Item(iIndex)
If (UCase(Trim(objNode.nodeName)) = UCase(sTag)) Then
XMLParser = objNode
Exit Function
Else
Set objNode = Nothing
End If
Next iIndex
End If

objTemp = objXML.getElementsByTagName(sTag) 'Search whole file with sTag
iTemp = objTemp.length
If (iTemp > 0) Then
objNode = objTemp.Item(0)
XMLParser = objNode
End If

End Function