Click here to Skip to main content
15,888,286 members
Articles / Programming Languages / Visual Basic
Article

Convert Number to Word

Rate me:
Please Sign up or sign in to vote.
2.98/5 (33 votes)
19 Feb 2008CPOL 202.4K   11K   34   39
Convert any decimal number to word using vb.net

Introduction

I have created this function for converting amount in Indian Rupees (INR). You can manipulate as you wish like decimal setting, Doller (any currency) Prefix.

Background

I have coded this function as per client's requirement to print word format of Billed Amount.

Using the code

This two dimensional array store the primary word convertion of number.

 Function retWord(ByVal Num As Decimal) As String
    'This two dimensional array store the primary word convertion of number.
    retWord = ""
    Dim ArrWordList(,) As Object = {{0, ""}, {1, "One"}, {2, "Two"}, {3, "Three"}, {4, "Four"}, _
                                    {5, "Five"}, {6, "Six"}, {7, "Seven"}, {8, "Eight"}, {9, "Nine"}, _
                                    {10, "Ten"}, {11, "Eleven"}, {12, "Twelve"}, {13, "Thirteen"}, {14, "Fourteen"}, _
                                    {15, "Fifteen"}, {16, "Sixteen"}, {17, "Seventeen"}, {18, "Eighteen"}, {19, "Nineteen"}, _
                                    {20, "Twenty"}, {30, "Thirty"}, {40, "Forty"}, {50, "Fifty"}, {60, "Sixty"}, _
                                    {70, "Seventy"}, {80, "Eighty"}, {90, "Ninety"}, {100, "Hundred"}, {1000, "Thousand"}, _
                                    {100000, "Lakh"}, {10000000, "Crore"}}

    Dim i As Integer
    For i = 0 To UBound(ArrWordList)
        If Num = ArrWordList(i, 0) Then
            retWord = ArrWordList(i, 1)
            Exit For
        End If
    Next
    Return retWord
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Pranav Patel
B.Sc, MCSD

I am serving my company as Sr. Software Engineer. I have more than 4 years of experience in Microsoft Technologies specially VB 6, VB.Net,C#,ASP.Net and MS SQL Server

Comments and Discussions

 
GeneralMy vote of 1 Pin
yogeshd3918-Feb-11 1:23
yogeshd3918-Feb-11 1:23 
GeneralGood Article but with alot of limitations Pin
Albert Dadze17-Dec-10 6:00
Albert Dadze17-Dec-10 6:00 
GeneralMy vote of 3 Pin
Ashwani2k12-Jul-10 8:14
Ashwani2k12-Jul-10 8:14 
GeneralUsefull Work Pin
Vimal Upadhyay16-May-10 22:43
Vimal Upadhyay16-May-10 22:43 
GeneralThanks Pin
hrivero23-Mar-10 9:35
hrivero23-Mar-10 9:35 
GeneralMy vote of 1 Pin
geminiousgoel26-Jan-10 5:45
geminiousgoel26-Jan-10 5:45 
GeneralC# Code please Pin
yeswanths4-May-09 4:36
yeswanths4-May-09 4:36 
GeneralRe: C# Code please - Working upto 999999999.99 Pin
Member 468636426-Nov-09 0:09
Member 468636426-Nov-09 0:09 
public class Num2Word
      {
            //////////////////////////////////////////////////////////////////////
            //Rewritten by Tapan Kumar Shanty                                                   //
            //Email: tapanshanty@yahoo.co.in                                                      //
            //Phone: +91 9985009005                                                                  //  
            //////////////////////////////////////////////////////////////////////

            public string strReplicate(string str, int intD)
            {
                  //This fucntion padded "0" after the number to evaluate hundred, thousand and on....
                  //using this function you can replicate any Charactor with given string.

                  string functionReturnValue = null;
                  int i = 0;
                  functionReturnValue = "";
                  for (i = 1; i <= intD; i++)
                  {
                        functionReturnValue = functionReturnValue + str;
                  }
                  return functionReturnValue;
            }

            public string AmtInWord(decimal Num)
            {
                  //I have created this function for converting amount in indian rupees (INR).
                  //You can manipulate as you wish like decimal setting, Doller (any currency) Prefix.

                  string functionReturnValue = null;
                  string strNum = null;
                  string strNumDec = null;
                  string StrWord = null;
                  strNum = Convert.ToString(Num);

                  if (strNum.IndexOf(".", 0) > -1)
                  {
                        strNumDec = strNum.Substring(strNum.IndexOf(".", 0) + 1);

                        if (strNumDec.Length == 1)
                        {
                              strNumDec = strNumDec + "0";
                        }
                        if (strNumDec.Length > 2)
                        {
                              strNumDec = strNumDec.Substring(1, 2);
                        }

                        if (strNum.IndexOf(".", 0) > -1)
                        {
                              strNum = strNum.Substring(0, strNum.IndexOf(".", 0));
                        }
                        StrWord = (Convert.ToDouble(strNum) == 1 ? " Rupee " : " Rupees ")
                              + NumToWord(Convert.ToDecimal(strNum))
                              + (Convert.ToDouble(strNumDec) > 0 ? " and " + cWord3(Convert.ToDecimal(strNumDec)) : "") + " Paise";
                  }
                  else
                  {
                        StrWord = (Convert.ToDouble(strNum) == 1 ? " Rupee " : " Rupees ") + NumToWord(Convert.ToDecimal(strNum));
                  }
                  functionReturnValue = StrWord + " Only";

                  return functionReturnValue;
            }

            public string NumToWord(decimal Num)
            {
                  //I divided this function in two part.
                  //1. Three or less digit number.
                  //2. more than three digit number.
                  string strNum = null;
                  string StrWord = null;
                  strNum = Convert.ToString(Num);

                  if (strNum.Length <= 3)
                  {
                        StrWord = cWord3(Convert.ToDecimal(strNum));
                  }
                  else
                        if (strNum.Length <= 5)
                        {

                              StrWord = cWordG3(Convert.ToDecimal(strNum.Substring(0, strNum.Length - 3))) + " " + cWord3(Convert.ToDecimal(strNum.Substring(strNum.Length - 3, 3)));
                        }
                        else
                              if (strNum.Length <= 7)
                              {

                                    StrWord = cWordG5(Convert.ToDecimal(strNum.Substring(0, strNum.Length - 5))) + "" + cWordG3(Convert.ToDecimal(strNum.Substring(strNum.Length - 5, 2))) + " " + cWord3(Convert.ToDecimal(strNum.Substring(strNum.Length - 3, 3)));
                              }
                              else
                                    if (strNum.Length <= 9)
                                    {

                                          StrWord = cWordG7(Convert.ToDecimal(strNum.Substring(0, strNum.Length - 7))) + "" + cWordG5(Convert.ToDecimal(strNum.Substring(strNum.Length - 7, 2))) + "" + cWordG3(Convert.ToDecimal(strNum.Substring(strNum.Length - 5, 2))) + " " + cWord3(Convert.ToDecimal(strNum.Substring(strNum.Length - 3, 3)));
                                    }
                  return StrWord;
            }

            public string cWordG3(decimal Num)
            {
                  string functionReturnValue = null;
                  //three to five digit number.
                  string strNum = "";
                  string StrWord = "";
                  string readNum = "";
                  strNum = Convert.ToString(Num);
                  if (strNum.Length % 2 != 0)
                  {
                        readNum = strNum.Substring(0, 1);
                        if (readNum != "0")
                        {
                              StrWord = retWord(Convert.ToDecimal(readNum));
                              readNum = 1 + strReplicate("0", strNum.Length - 1) + "000";
                              StrWord = StrWord + " " + retWord(Convert.ToDecimal(readNum));
                        }
                        strNum = strNum.Substring(1);
                  }
                  while (!(strNum.Length == 0))
                  {
                        readNum = strNum.Substring(0, 2);
                        if (readNum != "0")
                        {
                              StrWord = StrWord + " " + cWord3(Convert.ToDecimal(readNum));
                              readNum = 1 + strReplicate("0", strNum.Length - 2) + "000";
                              StrWord = StrWord + " " + retWord(Convert.ToDecimal(readNum));
                        }
                        strNum = strNum.Substring(2);
                  }
                  functionReturnValue = StrWord;
                  return functionReturnValue;
            }

            public string cWordG5(decimal Num)
            {
                  string functionReturnValue = null;
                  //Five to Seven digit number.
                  string strNum = "";
                  string StrWord = "";
                  string readNum = "";
                  strNum = Convert.ToString(Num);
                  if (strNum.Length % 2 != 0)
                  {
                        readNum = strNum.Substring(0, 1);
                        if (readNum != "0")
                        {
                              StrWord = retWord(Convert.ToDecimal(readNum));
                              readNum = 1 + strReplicate("0", strNum.Length - 1) + "00000";
                              StrWord = StrWord + " " + retWord(Convert.ToDecimal(readNum));
                        }
                        strNum = strNum.Substring(1);
                  }
                  while (!(strNum.Length == 0))
                  {
                        readNum = strNum.Substring(0, 2);
                        if (readNum != "0")
                        {
                              StrWord = StrWord + " " + cWord3(Convert.ToDecimal(readNum));
                              readNum = 1 + strReplicate("0", strNum.Length - 2) + "00000";
                              StrWord = StrWord + " " + retWord(Convert.ToDecimal(readNum));
                        }
                        strNum = strNum.Substring(2);
                  }
                  functionReturnValue = StrWord;
                  return functionReturnValue;
            }

            public string cWordG7(decimal Num)
            {
                  string functionReturnValue = null;
                  //Five to Seven digit number.
                  string strNum = "";
                  string StrWord = "";
                  string readNum = "";
                  strNum = Convert.ToString(Num);
                  if (strNum.Length % 2 != 0)
                  {
                        readNum = strNum.Substring(0, 1);
                        if (readNum != "0")
                        {
                              StrWord = retWord(Convert.ToDecimal(readNum));
                              readNum = 1 + strReplicate("0", strNum.Length - 1) + "0000000";
                              StrWord = StrWord + " " + retWord(Convert.ToDecimal(readNum));
                        }
                        strNum = strNum.Substring(1);
                  }
                  while (!(strNum.Length == 0))
                  {
                        readNum = strNum.Substring(0, 2);
                        if (readNum != "0")
                        {
                              StrWord = StrWord + " " + cWord3(Convert.ToDecimal(readNum));
                              readNum = 1 + strReplicate("0", strNum.Length - 2) + "0000000";
                              StrWord = StrWord + " " + retWord(Convert.ToDecimal(readNum));
                        }
                        strNum = strNum.Substring(2);
                  }
                  functionReturnValue = StrWord;
                  return functionReturnValue;
            }

            public string cWord3(decimal Num)
            {
                  string functionReturnValue = null;
                  //1. Three or less digit number.
                  string strNum = "";
                  string StrWord = "";
                  string readNum = "";
                  if (Num < 0) Num = Num * -1;
                  strNum = Convert.ToString(Num);

                  if (strNum.Length == 3)
                  {
                        readNum = strNum.Substring(0, 1);
                        StrWord = retWord(Convert.ToDecimal(readNum)) + " Hundred";
                        strNum = strNum.Substring(1, strNum.Length - 1);
                  }

                  if (strNum.Length <= 2)
                  {
                        if (Convert.ToDecimal(strNum) >= 0 & Convert.ToDecimal(strNum) <= 20)
                        {
                              StrWord = StrWord + " " + retWord(Convert.ToDecimal(strNum));
                        }
                        else
                        {
                              StrWord = StrWord + " " + retWord(Convert.ToDecimal(strNum.Substring(0, 1) + "0")) + " " + retWord(Convert.ToDecimal(strNum.Substring(1, 1)));
                        }
                  }

                  strNum = Convert.ToString(Num);
                  functionReturnValue = StrWord;
                  return functionReturnValue;
            }

            public string retWord(decimal Num)
            {
                  string functionReturnValue = null;
                  //This two dimensional array store the primary word convertion of number.
                  functionReturnValue = "";
                  object[,] ArrWordList = { { 0, "" }, { 1, "One" }, { 2, "Two" }, { 3, "Three" }, { 4, "Four" }, { 5, "Five" }, { 6, "Six" }, { 7, "Seven" }, { 8, "Eight" }, { 9, "Nine" },
                  { 10, "Ten" }, { 11, "Eleven" }, { 12, "Twelve" }, { 13, "Thirteen" }, { 14, "Fourteen" }, { 15, "Fifteen" }, { 16, "Sixteen" }, { 17, "Seventeen" }, { 18, "Eighteen" }, { 19, "Nineteen" },
                  { 20, "Twenty" }, { 30, "Thirty" }, { 40, "Forty" }, { 50, "Fifty" }, { 60, "Sixty" }, { 70, "Seventy" }, { 80, "Eighty" }, { 90, "Ninety" }, { 100, "Hundred" }, { 1000, "Thousand" },
                  { 100000, "Lakh" }, { 10000000, "Crore" } };

                  int i = 0;
                  for (i = 0; i <= ArrWordList.Length; i++)
                  {
                        if (Num == Convert.ToDecimal(ArrWordList[i, 0]))
                        {
                              functionReturnValue = Convert.ToString(ArrWordList[i, 1]);
                              break; // TODO: might not be correct. Was : Exit For
                        }
                  }
                  return functionReturnValue;
            }
      }
GeneralThanks a lot Pin
rajakar0123-Dec-08 19:00
rajakar0123-Dec-08 19:00 
GeneralTry One million and above Pin
sensei41200131-Aug-08 22:20
sensei41200131-Aug-08 22:20 
GeneralThanx a lot Pin
Amit Code Project19-Aug-08 2:24
Amit Code Project19-Aug-08 2:24 
GeneralThanks Pin
csiconnect12-Aug-08 3:06
csiconnect12-Aug-08 3:06 
GeneralThanks Pin
Rai Shahid15-Jul-08 23:38
Rai Shahid15-Jul-08 23:38 
GeneralIndRs. converter PinPopular
Arijit Manna15-Jul-08 2:31
Arijit Manna15-Jul-08 2:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.