Developing an XML Web Service Using Visual Studio 2005 - Developing the XML Web Service continued
(Page 5 of 6 )
Now, let us proceed with the “Process3DigitNumber” method, which is usually called “ConvertToWords”.
private string Process3DigitNumber(string sSource)
{
string sRetVal = "";
//Make sure the source should be three digits
sSource = "000" + sSource;
sSource = sSource.Substring(sSource.Length - 3);
//Hundred possion
if (sSource.Substring(0, 1) != "0")
sRetVal = ProcessSingleDigitNumber(sSource.Substring(0, 1)) + " Hundred ";
//Tens and ones possion
if (sSource.Substring(1, 1) != "0")
sRetVal += Process2DigitNumber(sSource.Substring(1));
else
sRetVal += ProcessSingleDigitNumber(sSource.Substring(2));
return sRetVal;
}
Once the above method receives a number (as a string), we left pad it with zeroes to make sure that it is a three digit number. After that, we chunk it further and send it to “Process2DigitNumber” or “ProcessSingleDigitNumber”, which are implemented as follows:
private string Process2DigitNumber(string sSource)
{
string sRetVal = "";
if (sSource.Substring(0, 1) == "1")
{
// Process 10 to 19
switch (sSource)
{
case "10": sRetVal = "Ten"; break;
case "11": sRetVal = "Eleven"; break;
case "12": sRetVal = "Twelve"; break;
case "13": sRetVal = "Thirteen"; break;
case "14": sRetVal = "Fourteen"; break;
case "15": sRetVal = "Fifteen"; break;
case "16": sRetVal = "Sixteen"; break;
case "17": sRetVal = "Seventeen"; break;
case "18": sRetVal = "Eighteen"; break;
case "19": sRetVal = "Nineteen"; break;
}
}
else
{
// Process 20 to 99
switch (sSource.Substring(0, 1))
{
case "2": sRetVal = "Twenty "; break;
case "3": sRetVal = "Thirty "; break;
case "4": sRetVal = "Forty "; break;
case "5": sRetVal = "Fifty "; break;
case "6": sRetVal = "Sixty "; break;
case "7": sRetVal = "Seventy "; break;
case "8": sRetVal = "Eighty "; break;
case "9": sRetVal = "Ninety "; break;
}
//Append single digit word
sRetVal += ProcessSingleDigitNumber(sSource.Substring(1));
}
return sRetVal;
}
I hope I don't need to explain much about the above method. It simply returns the string based on the two digit value sent to it. This method may further call “ProcessSingleDigitNumber” to process only single digits. Let us see how that is implemented.
private string ProcessSingleDigitNumber(string sSource)
{
string sRetVal = "";
switch (sSource)
{
case "1": sRetVal = "One"; break;
case "2": sRetVal = "Two"; break;
case "3": sRetVal = "Three"; break;
case "4": sRetVal = "Four"; break;
case "5": sRetVal = "Five"; break;
case "6": sRetVal = "Six"; break;
case "7": sRetVal = "Seven"; break;
case "8": sRetVal = "Eight"; break;
case "9": sRetVal = "Nine"; break;
default: sRetVal = ""; break;
}
return sRetVal;
}
And thus, our coding (development) part is successfully finished.
Next: Executing and testing the XML Web Service >>
More Visual Basic Articles
More By Jagadish Chaterjee