Make an extention method that converts a number into words (as in "one thousand eight hundred twelve") in C#

For this example, I wrote an extension method that adds a ToWords function to the double class. The function is in the static class DoubleExtensions.

The following code shows the extension method ToWords.

// Convert a double into words. 
// E.g. "one thousand eight hundred twelve."
public static string ToWords(this double num)
{
// Return a word representation of the whole number value.
// Remove any fractional part.
num = Math.Truncate(num);

// If the number is 0, return zero.
if (num == 0) return "zero";

string[] groups = {"", "thousand", "million", "billion",
"trillion", "quadrillion", "?", "??", "???", "????"};
string result = "";

// Process the groups, smallest first.
int group_num = 0;
while (num > 0)
{
// Get the next group of three digits.
double quotient = Math.Truncate(num / 1000);
int remainder = (int)Math.Round(num - quotient * 1000);
num = quotient;

// Convert the group into words.
result = GroupToWords(remainder) +
" " + groups[group_num] + ", " +
result;

// Get ready for the next group.
group_num++;
}

// Remove the trailing ", ".
if (result.EndsWith(", "))
result = result.Substring(0, result.Length - 2);

return result.Trim();
}

Function NumberToString returns a string representation of a longer whole number. If the number is 0, it simply returns "zero."

The function enters a loop to process each group of three digits: ones, thousands, millions, and so forth. Each time through the loop, the function gets the next group's value. It calls function GroupToWords to get a string representation of the group and adds the appropriate group name after it (e.g. thousands).

Function GroupToWords returns a string representing a number between 0 and 999.

// Convert a number between 0 and 999 into words.
private static string GroupToWords(int num)
{
string[] one_to_nineteen = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eightteen", "nineteen"};
string[] multiples_of_ten = {"twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};

// If the number is 0, return an empty string.
if (num == 0) return "";

// Handle the hundreds digit.
int digit;
string result = "";
if (num > 99)
{
digit = (int)(num / 100);
num = num % 100;
result = one_to_nineteen[digit] + " hundred";
}

// If num = 0, we have hundreds only.
if (num == 0) return result.Trim();

// See if the rest is less than 20.
if (num < 20)
{
// Look up the correct name.
result += " " + one_to_nineteen[num];
}
else
{
// Handle the tens digit.
digit = (int)(num / 10);
num = num % 10;
result += " " + multiples_of_ten[digit - 2];

// Handle the final digit.
if (num > 0)
result += " " + one_to_nineteen[num];
}

return result.Trim();
}

If the number is 0, function GroupToWords simply returns "zero."

Next if the number is greater than 99, the function gets the hundreds digit and uses the one_to_nineteen array to look up its string representation. It adds that representation plus the word "hundred" to the result. If the number has no other digits, the function returns what it has so far.

Next if the remaining number is less than 20, the function uses the one_to_nineteen array to look up the number's representation.

If the number is greater than 19, the function gets the tens digit and uses the multiples_of_ten array to get its representation. It then uses the one_to_nineteen array to add on the representation of the ones digit.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.