1. 2012
    Jun
    28

    Convert Number to Word Utility

    Posted By


    word number one

    The inspiration for this solution came from an interview question do a function that allows number to be converted to words. So if you have a number like 3402 you should have as your result “three thousand four hundred and two”. This problem is not new, but I was intrigued to see what a solution would look like. I have done the solution in C#.

        public static class Number
        {
            public enum Type { Short, Long };
    
            public static string ToWord(this int inputInteger)
            {
                if (inputInteger == 0)
                    return "zero";
    
                if (inputInteger < 0)
                    return "minus " + ToWord(Math.Abs(inputInteger));
    
                var words = "";
    
                if ((inputInteger / 1000000) > 0)
                {
                    words += ToWord(inputInteger / 1000000) + " million ";
                    inputInteger %= 1000000;
                }
    
                if ((inputInteger / 1000) > 0)
                {
                    words += ToWord(inputInteger / 1000) + " thousand ";
                    inputInteger %= 1000;
                }
    
                if ((inputInteger / 100) > 0)
                {
                    words += ToWord(inputInteger / 100) + " hundred ";
                    inputInteger %= 100;
                }
    
                if (inputInteger > 0)
                {
                    if (words != "")
                        words += "and ";
    
                    var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
                    var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    
                    if (inputInteger < 20)
                        words += unitsMap[inputInteger];
                    else
                    {
                        words += tensMap[inputInteger / 10];
                        if ((inputInteger % 10) > 0)
                            words += "-" + unitsMap[inputInteger % 10];
                    }
                }
    
                return words;
    
            }
            
            public static string ToWord(this long inputInteger)
            {
                return ToWord(inputInteger, Type.Short);
            }
    
            public static string ToWord(this long inputInteger, Type type)
            {
    
                if (inputInteger == 0)
                    return "zero";
    
                if (inputInteger < 0)
                    return "minus " + ToWord(Math.Abs(inputInteger), type);
    
                var words = "";
    
                if (type == Type.Long)
                {
    
                    if ((inputInteger / 1000000000000000000) > 0)
                    {
                        words += ToWord(inputInteger / 1000000000000000000, type) + " trillion ";
                        inputInteger %= 1000000000000000000;
                    }
    
                    if ((inputInteger / 1000000000000 ) > 0)
                    {
                        words += ToWord(inputInteger / 1000000000000, type) + " billion ";
                        inputInteger %= 1000000000000;
                    }
    
                } else {
    
                    if ((inputInteger / 1000000000000) > 0)
                    {
                        words += ToWord(inputInteger / 1000000000000, type) + " trillion ";
                        inputInteger %= 1000000000000;
                    }
    
                    if ((inputInteger / 1000000000) > 0)
                    {
                        words += ToWord(inputInteger / 1000000000, type) + " billion ";
                        inputInteger %= 1000000000;
                    }
    
                }
    
                return  (inputInteger > 1000000000)
                    ? words + inputInteger.ToWord()
                    : words + Convert.ToInt32(inputInteger).ToWord();
    
            }
    
        }
    

    The solution is not entirely new, this solution would be likely useful in the banking sector where they use both the integer number and the worded number. I consider this to be more of a utility method the same way you would want to use int.ToString() the method should be used in the same way.

    
    const int test = 3402;
    test.ToWord();
    

    Another advantage of using the method it takes into account for long and short scales for the two of the several different large-numbering nameing systems.

    
    const long test = 4090300807009004104;
    
    test.ToWord(Number.Type.Long);
    // result: four trillion ninety thousand three hundred billion eight hundred and seven billion nine million four thousand one hundred and four
    
    test.ToWord(Number.Type.Short);
    // result: four million ninety thousand three hundred trillion eight hundred and seven billion nine million four thousand one hundred and four
    

    An obvious disadvantage of the method is that you are limited by the range allowed –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 for a long type.


  2. About Emeka Echeruo

    Emeka Echeruo

    I love sports, football which I refuse to call soccer, and the outdoor especially walks in park. Software development is my passion, there is a beauty in creating something out of nothing but algebra that ends up becomes a part of a persons daily life. I love kids, dogs, nightlife and art because it finds you and moves you emotionally!

  3. Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.