SIGN UP MEMBER LOGIN:    
ARTICLE

Converting Numbers to Words using the Indian Numbering System

Posted by Vulpes Articles | C# Language May 22, 2011
In this article, I'd like to present a similar program using the Indian numbering system. This system differs from the US/UK systems in that it doesn't use 'millions' but instead uses 'lakhs' and 'crores' to represent multiples of 100 thousand and 10 million respectively.
Reader Level:

Introduction

In an earlier article (http://www.c-sharpcorner.com/UploadFile/b942f9/6362/?ArticleID=0910264a-898f-49be-84b5-38b5b5f83a15) , I presented a simple program for converting numbers to words using the US or UK numbering systems. The program was restricted to integers in the range of an Int32 (about plus or minus 2 billion) which I believe is the most useful case.

In this article, I'd like to present a similar program using the Indian numbering system. This system differs from the US/UK systems in that it doesn't use 'millions' but instead uses 'lakhs' and 'crores' to represent multiples of 100 thousand and 10 million respectively.

A billion can be represented by the term 'arab' though I understand that this is seldom used in practice and most Indian people prefer to say 100 crore.
However, for the sake of completeness, the program has an option to use 'arab' and also to separate different number denominations with 'and' as in the UK numbering system.

Source Code

using System;

class Program
{
    static void Main()
    {
        string input;
        int number;
        bool isValid;
        bool useAnd = false;
        bool useArab = false;

        Console.WriteLine("\nEnter '0' to quit the program at any time\n");

        while (true)
        {
            Console.Write("\nUse 'and' as separator y/n : ");
            input = Console.ReadLine();
            if (!(input.ToLower() == "y" || input.ToLower() == "n"))
                Console.WriteLine("\n  Must be 'y' or 'n', please try again\n");
            else
            {
                if (input.ToLower() == "y") useAnd = true;
                Console.WriteLine();
                break;
            }
        }

        while (true)
        {
            Console.Write("\nUse 'Arab' instead of '100 Crore' y/n : ");
            input = Console.ReadLine();
            if (!(input.ToLower() == "y" || input.ToLower() == "n"))
                Console.WriteLine("\n  Must be 'y' or 'n', please try again\n");
            else
            {
                if (input.ToLower() == "y") useArab = true;
                Console.WriteLine("\n");
                break;
            }
        }         

        do
        {
            Console.Write("Enter integer : ");
            input = Console.ReadLine();
            isValid = int.TryParse(input, out number);
            if (!isValid)
                Console.WriteLine("\n Not an integer, please try again\n");
            else
                Console.WriteLine("\n {0}\n", NumberToText(number, useAnd, useArab));
        }
        while (!(isValid && number == 0));

        Console.WriteLine("\nProgram ended");
    }

    public static string NumberToText(int number, bool useAnd, bool useArab)
    {
        if (number == 0) return "Zero";

        string and = useAnd ? "and " : ""; // deals with using 'and' separator

        if (number == -2147483648) return "Minus Two Hundred " + and + "Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred " + and +"Forty Eight";

        int[] num = new int[4];
        int first = 0;
        int u, h, t;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        if (number < 0)
        {
            sb.Append("Minus ");
            number = -number;
        }
        string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ", "Five " ,"Six ", "Seven ", "Eight ", "Nine "};
        string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "};
        string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ","Eighty ", "Ninety "};
        string[] words3 = { "Thousand ", "Lakh ", "Crore " };
        num[0] = number % 1000; // units
        num[1] = number / 1000;
        num[2] = number / 100000;
        num[1] = num[1] - 100 * num[2]; // thousands
        num[3] = number / 10000000; // crores
        num[2] = num[2] - 100 * num[3]; // lakhs
        for (int i = 3; i > 0; i--)
        {
            if (num[i] != 0)
            {
                first = i;
                break;
            }
        }

        for (int i = first; i >= 0; i--)
        {
            if (num[i] == 0) continue;

            u = num[i] % 10; // ones
            t = num[i] / 10;
            h = num[i] / 100; // hundreds
            t = t - 10 * h;
// tens

            if (h > 0) sb.Append(words0[h] + "Hundred ");
            if (u > 0 || t > 0)
            {
                if (h > 0 || i < first) sb.Append(and);

                if (t == 0)
                    sb.Append(words0[u]);
                else if (t == 1)
                    sb.Append(words1[u]);
                else
                    sb.Append(words2[t - 2] + words0[u]);
            }
            if (i != 0) sb.Append(words3[i - 1]);
        }

        string temp = sb.ToString().TrimEnd();

        if (useArab && Math.Abs(number) >= 1000000000)
        {
            int index = temp.IndexOf("Hundred Crore");
            if (index > -1) return temp.Substring(0, index) + "Arab" + temp.Substring(index + 13);
            index = temp.IndexOf("Hundred");            return temp.Substring(0, index) + "Arab" + temp.Substring(index + 7);
        }
        return temp;

    }
}

Conclusion

As in the case of the previous program, it would not be difficult to extend this program to deal with larger integers or decimal numbers for anyone who needs this functionality.
 

Login to add your contents and source code to this article
share this article :
post comment
 

I did it sir I did string search in given input no for (.) and then split it in two different no and then use same code for both no's and then concatenate these two nos by " point" word

Posted by Mayur Gujrathi Jul 20, 2011

As mentioned in the Conclusion, the code doesn't deal with decimal numbers. However, I'll be posting a blog later today which contains a quick fix for doing so.

Posted by Vulpes Jul 20, 2011

Dear sir i am occuring problem when i am giving input like this 1.7

Posted by Mayur Gujrathi Jul 20, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor