Numbers to Words 🚀Converter Using C#

R M Shahidul Islam Shahed
3 min readFeb 12, 2024
Numbers to Words 🚀Converter Using C#

To create a Numbers to Words Converter in C#, you can follow these steps:

  1. Define a method to convert individual digits to words.
  2. Implement a method to handle numbers up to a certain magnitude (e.g., millions, billions).
  3. Break down the input number into groups of three digits.
  4. Recursively convert each group to words, considering its magnitude.
  5. Handle special cases like zero, tens, and tens.
  6. Assemble the converted words with appropriate magnitudes (e.g., thousand, million).
  7. Handle edge cases and exceptions gracefully.

Here’s a basic implementation of a Numbers to Words Converter in C#:

Numbers to Words 🚀Converter Using C#

This implementation allows you to convert numbers up to trillions into words. You can adjust the Ones, Tens, Tens, and Thousands arrays to support additional languages or customize the output format.

public static class Service
{
private static readonly string[] Ones = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
private static readonly string[] Teens = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
private static readonly string[] Tens = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
private static readonly string[] Thousands = { "", "Thousand", "Million", "Billion", "Trillion" };

public static string ConvertToWords(long number)
{
if (number == 0)
return "Zero";

string words = "";

for (int i = 0; number > 0; i++)
{
if (number % 1000 != 0)
words = ConvertHundreds(number % 1000) + Thousands[i] + " " + words;

number /= 1000;
}

return words.Trim();
}

private static string ConvertHundreds(long number)
{
string words = "";

if (number >= 100)
{
words += Ones[number / 100] + " Hundred ";
number %= 100;
}

if (number >= 10 && number <= 19)
{
words += Teens[number % 10] + " ";
}
else
{
words += Tens[number / 10] + " ";
words += Ones[number % 10] + " ";
}

return words;
}
}

Program Output:

using NumberToWords;

Console.Write("Enter a number:");
long number = Convert.ToInt64(Console.ReadLine());
Console.WriteLine("In word:" + Service.ConvertToWords(number));

In conclusion, implementing a Numbers to Words Converter using the C# programming language provides a practical tool for converting numerical representations into their corresponding verbal equivalents. The solution outlined utilizes a systematic approach, employing arrays to map individual digits, tens, and special cases such as teens and multiples of ten. By breaking down input numbers into manageable groups and recursively converting them to words, the converter accommodates a wide range of numerical magnitudes, up to trillions. Through structured logic and modular design, the C# program ensures efficiency, accuracy, and flexibility in handling diverse numerical inputs. This converter serves as a valuable resource for applications requiring human-readable representations of numerical data, facilitating clearer communication and enhanced user experience.

đź‘‹ .NET Application Collections
🚀 My Youtube Channel
đź’» Github

--

--