So far, we have seen how to convert the two-digit number into words. If the number contains more than two digits, how will we convert it? For this, I am going to write a function, which accepts a number up to 12 digits as a string and returns as a converted word string. The function is shown below.
- private static String ConvertWholeNumber(String Number)
- {
- string word = "";
- try
- {
- bool beginsZero = false;
- bool isDone = false;
- double dblAmt = (Convert.ToDouble(Number));
-
- if (dblAmt > 0)
- {
- beginsZero = Number.StartsWith("0");
-
- int numDigits = Number.Length;
- int pos = 0;
- String place = "";
- switch (numDigits)
- {
- case 1:
-
- word = ones(Number);
- isDone = true;
- break;
- case 2:
- word = tens(Number);
- isDone = true;
- break;
- case 3:
- pos = (numDigits % 3) + 1;
- place = " Hundred ";
- break;
- case 4:
- case 5:
- case 6:
- pos = (numDigits % 4) + 1;
- place = " Thousand ";
- break;
- case 7:
- case 8:
- case 9:
- pos = (numDigits % 7) + 1;
- place = " Million ";
- break;
- case 10:
- case 11:
- case 12:
-
- pos = (numDigits % 10) + 1;
- place = " Billion ";
- break;
-
- default:
- isDone = true;
- break;
- }
- if (!isDone)
- {
- if (Number.Substring(0, pos) != "0" && Number.Substring(pos) != "0")
- {
- try
- {
- word = ConvertWholeNumber(Number.Substring(0, pos)) + place + ConvertWholeNumber(Number.Substring(pos));
- }
- catch { }
- }
- else
- {
- word = ConvertWholeNumber(Number.Substring(0, pos)) + ConvertWholeNumber(Number.Substring(pos));
- }
-
-
-
- }
-
- if (word.Trim().Equals(place.Trim())) word = "";
- }
- }
- catch { }
- return word.Trim();
- }
Here, as you can see this function is also same as the previous one buta little bit changes here. In this, we switch case. I am checking up to 12 digits due to which there are 12 cases, if you know more than that case, then you can have 12 cases. Here, if you pass more than 12 digits, it will return empty as a word because by default, I am updating isDone Boolean to true, then it will return nothing. Here, if you pass a single digit, then it will go to case one and calls step1 function ones and it returns and I am updating isDone Boolean to true, so it can return from this function as well and for two-digit numbers, it is also the same but for cases, where there are more than two digits, I didn’t update isDone Boolean. Thus, after executing that case, it comes to if condition. Here, I am recursively calling this function again which is based on the position, until isDone Boolean updates to true.
For example I am passing the number 4568, then in this function, case 4 will execute first because it has 4 digits and in case 4, you will get pos as (4568%4) +1 =0+1=1, place =” Thousand”
It comes to if condition and recursively calls it, which is based on this position.
Thus, it calls for substring from 0 to pos i.e. 1 character -- that is, you will get 4.
Thus, it returns “Four” + “Thousand”+ again.
At last, we will get an output as “Four Thousand Five Hundred Sixty Eight”.
This is how we can convert the whole number with 12 digit convert to words.