Everyone Is An Achiever

Wednesday, January 14, 2015

Represent a number as a character string

1:21 AM Posted by Unknown 7 comments


Hello everyone,
Today we'll do something simple, cool and useful. Today we'll learn how to get the character representation of a number, for example: the number: 2456 is "Two Thousand Four Hundred Fifty Six"
You need such feature whenever you deal with bills and invoices.
Before we go to the code (although it could be lengthy but extremely easy) let's talk in theory first.
whenever we have a number, for example 2456, we pronounce the number starting from the left most to the right most; the 2, then 4, then 5, then 6. And for sure you know the place value of 2 which thousands, 4 is hundreds, 5 tens and lastly 6 which is the ones place. So, in our code we will 
1- accept the number from the user
2- split the number into digits, see [improved] Convert a whole integer to an array of integers
3- start reading the first location (that is 2 in this example)
4- store the corresponding character string of the number (Two Thousand in this example)
5- remove it so that the next number will hold the first location
6- keep reading the first location and storing the corresponding character strings one after the other until you reach the last number

You can think of this going in a hierarchy from the top level (left most number) to the first level (right most number)
In the following program 4 functions, the first one is for single digits (the ones place value) the second is for 2 digits (the tens place value), the third is for 3 digits (the hundred place value), the fourth is for 4 digits ( the thousands place value). And I created one extra function as a special case and I called it teen family which is for the numbers from 10 to 19.

How it works?
1- we first determine how many digits is the number?
2- we start from the top level,e.g. 2456 we'll start from the the 4 digits function
3- after storing the value of the first number, we remove it from the list and call the second downward function
4- in the 3 digit function we check whether the remaining numbers are in the category of teen family, if yes then we call the teen family instead of two digit function, if not we call the two digit function
5- the process continue with the next level downward until we reach the single digit function
6- lastly, the alphabetical representation of the number is returned to the user

NOTE that the code will work for number between 1 up to 9999. You can extend the code more if you like :)

lets, look at the code
1- main() code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumbertoAlphabetsRepresentaiton
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number:");
            string number = Console.ReadLine();
            ConvertToIntegers conv = new ConvertToIntegers();
            byte[] numberArray = conv.doConvert(number);
            
            NumberToAlphabets nObj = new NumberToAlphabets();
            Console.WriteLine(nObj.Return(numberArray));

            Console.ReadLine();
        }
    }
}
2- NumberToAlphabets code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumbertoAlphabetsRepresentaiton
{
    class NumberToAlphabets
    {
        static string numberString = "";
        
       
        public string Return(byte[] number)
        {
            // we first creat a list and copy the number to it
            // the list is automatically rezieable which is what we look for here
            List list = new List();
            for (int i = 0; i < number.Length; i++)
                list.Add(number[i]);

            int numberOfDigits = list.Count;
            //then we start by calling the correct function "the top most"
            if (numberOfDigits == 1)
                singleDigit(list);
            else if (numberOfDigits == 2)
                twoDigit(list);
            else if (numberOfDigits == 3)
                threeDigit(list);
            else if(numberOfDigits == 4)
                 fourDigit(list);

            return numberString;
        }
        //notice that we only check the location ZERO [0]
        //because this location will has the number we need to check
        //and after reading it we remove location [0] making the next number to hold this position
        private void singleDigit(List number)
        {
             if (number[0] == 1)
                numberString += "One";
            else if (number[0] == 2)
                numberString += "Two";
            else if (number[0] == 3)
                numberString += "Three";
            else if (number[0] == 4)
                numberString += "Four";
            else if (number[0] == 5)
                numberString += "Five";
            else if (number[0] == 6)
                numberString += "Six";
            else if (number[0] == 7)
                numberString += "Seven";
            else if (number[0] == 8)
                numberString += "Eight";
            else if (number[0] == 9)
                numberString += "Nine";
        }
        private void twoDigit(List number)
        {
           
            if (number[0] == 2)
                numberString += "Twinty ";
            else if (number[0] == 3)
                numberString += "Thirty ";
            else if (number[0] == 4)
                numberString += "Fourty ";
            else if (number[0] == 5)
                numberString += "fifty ";
            else if (number[0] == 6)
                numberString += "Sixty ";
            else if (number[0] == 7)
                numberString += "Seventy ";
            else if (number[0] == 8)
                numberString += "Eighty ";
            else if (number[0] == 9)
                numberString += "Ninty ";
            number.RemoveAt(0);
            singleDigit(number);
        }
        
        private void threeDigit(List number)
        {
            if (number[0] == 1)
                numberString += "One Hundred ";
            else if (number[0] == 2)
                numberString += "Two Hundred ";
            else if (number[0] == 3)
                numberString += "Three Hundred ";
            else if (number[0] == 4)
                numberString += "Four Hundred ";
            else if (number[0] == 5)
                numberString += "Five Hundred ";
            else if (number[0] == 6)
                numberString += "Six Hundred ";
            else if (number[0] == 7)
                numberString += "Seven Hundred ";
            else if (number[0] == 8)
                numberString += "Eight Hundred ";
            else if (number[0] == 9)
                numberString += "Nine Hundred ";
           
            number.RemoveAt(0);
            //here we check whether the rest number are between 10 and 19
            // if yes then we call the teenFamily() function
            //if the remaining number lets say [1][2] or [1][7]
            //we only check whther the location ZERO is == to 1
            //if yes then it is definitly a teen number
            if (number[0] == 1)
            {
                //in the list the numbers are stored individually [1][7]
                //and since we need the number to in full form lets say seventeen 17,
                //we add the first number + the second number + 9 => or you can add the second lcation [7] with 10 ;)
                int N = number[0] + number[1] + 9;
                    teenFamily(N);
            }
            else
                twoDigit(number);

        }
        private void fourDigit(List number)
        {
            if (number[0] == 1)
                numberString += "One Thousand ";
            else if (number[0] == 2)
                numberString += "Two Thousand ";
            else if (number[0] == 3)
                numberString += "Three Thousand ";
            else if (number[0] == 4)
                numberString += "Four Thousand ";
            else if (number[0] == 5)
                numberString += "Five Thousand ";
            else if (number[0] == 6)
                numberString += "Six Thousand ";
            else if (number[0] == 7)
                numberString += "Seven Thousand ";
            else if (number[0] == 8)
                numberString += "Eight Thousand ";
            else if (number[0] == 9)
                numberString += "Nine Thousand ";

            number.RemoveAt(0);
            threeDigit(number);
        }

        private void teenFamily(int teenNumber)
        {
            if (teenNumber == 10)
                numberString += "Ten ";
            else if (teenNumber == 11)
                numberString += "Eleven ";
            else if (teenNumber == 12)
                numberString += "Twilve ";
            else if (teenNumber == 13)
                numberString += "Thirteen ";
            else if (teenNumber == 14)
                numberString += "Fourteen ";
            else if (teenNumber == 15)
                numberString += "Fifteen ";
            else if (teenNumber == 16)
                numberString += "Sixteen ";
            else if (teenNumber == 17)
                numberString += "Seventeen ";
            else if (teenNumber == 18)
                numberString += "Eighteen ";
            else if (teenNumber == 19)
                numberString += "Ninteen ";
          
        }


    }
}

3- ConvertToIntegers code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumbertoAlphabetsRepresentaiton
{
    class ConvertToIntegers
    {
        private byte[] numbers { get; set; }
        
        public byte [] doConvert(string strNumber)
        {
            numbers = new byte[strNumber.Length];
            for (int i = 0; i < numbers.Length; i++)
                numbers[i] =(byte)Convert.ToChar(strNumber[i] - '0');

            return numbers;
        }
        
    }
}


Download the solution here.
That's all. hope you enjoyed it.
Your comments are valuable to me and I'll be happy to read them.

7 comments:

  1. Hi, this intrigued me and I wondered if I could create something similar that can be expanded upon by simply adding new items to the dictionaries of "known terms". This is what I came up with. What do you think?

    https://gist.github.com/jbatte47/5fee8663d411d3f5ca37

    ReplyDelete
    Replies
    1. well, I'm impressed. That's what I call professional programming

      Delete
    2. Thanks! I'm glad you liked it :)

      Delete
  2. Crap...

    What happens to a number greater than 4 digits?? You again have to go through this whole exercise?? Try to find a real generic way which can handle any number of digits... Millions, billions, trillions...

    ReplyDelete
    Replies
    1. My gist (one comment before yours) does a max value of 999,999,999,999,999, but it still has issues: no decimal handling, for instance. Might be a good place to start though.

      Delete