This is a program I ended up doing for a homework assignment back in the day. It does serve some practical purpose on occasion, but is also a great learning tool for recursion. I did take a little bit of time to go back through and have the program handle positive and negative doubles and quite large ones at that. One practical application you might see this used in could be check writing.

This is a C# Console Application code so simply load up visual studios (or your favorite C# compiler) and drop this code on into a Console application .cs file. Click Run, and you're ready to start converting.

``````using System;
using System.Collections.Generic;
using System.Text;

namespace NumWords
{
class Program
{
// PROGRAM HANDLES NEGATIVE AND POSITIVE DOUBLES

static String NumWordsWrapper(double n)
{
string words = "";
double intPart;
double decPart = 0;
if (n == 0)
return "zero";
try {
string[] splitter = n.ToString().Split('.');
intPart = double.Parse(splitter[0]);
decPart = double.Parse(splitter[1]);
} catch {
intPart = n;
}

words = NumWords(intPart);

if (decPart > 0) {
if (words != "")
words += " and ";
int counter = decPart.ToString().Length;
switch (counter) {
case 1: words += NumWords(decPart) + " tenths"; break;
case 2: words += NumWords(decPart) + " hundredths"; break;
case 3: words += NumWords(decPart) + " thousandths"; break;
case 4: words += NumWords(decPart) + " ten-thousandths"; break;
case 5: words += NumWords(decPart) + " hundred-thousandths"; break;
case 6: words += NumWords(decPart) + " millionths"; break;
case 7: words += NumWords(decPart) + " ten-millionths"; break;
}
}
return words;
}

static String NumWords(double n) //converts double to words
{
string[] numbersArr = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string[] tensArr = new string[] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" };
string[] suffixesArr = new string[] { "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion", "Novemdecillion", "Vigintillion" };
string words = "";

bool tens = false;

if (n < 0) {
words += "negative ";
n *= -1;
}

int power = (suffixesArr.Length + 1) * 3;

while (power > 3) {
double pow = Math.Pow(10, power);
if (n >= pow) {
if (n % pow > 0) {
words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
} else if (n % pow == 0) {
words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
}
n %= pow;
}
power -= 3;
}
if (n >= 1000) {
if (n % 1000 > 0) words += NumWords(Math.Floor(n / 1000)) + " thousand, ";
else words += NumWords(Math.Floor(n / 1000)) + " thousand";
n %= 1000;
}
if (0 <= n && n <= 999) {
if ((int)n / 100 > 0) {
words += NumWords(Math.Floor(n / 100)) + " hundred";
n %= 100;
}
if ((int)n / 10 > 1) {
if (words != "")
words += " ";
words += tensArr[(int)n / 10 - 2];
tens = true;
n %= 10;
}

if (n < 20 && n > 0) {
if (words != "" && tens == false)
words += " ";
words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
n -= Math.Floor(n);
}
}

return words;

}
static void Main(string[] args)
{
Console.Write("Enter a number to convert to words: ");

Console.WriteLine("{0}", NumWordsWrapper(n));
}
}
}``````

Posted by H Das on
Sebd
Posted by Biruk on
Great Work man.
Posted by Brian on
If you dont mind, I made some modifications to allow for decimal places:
(Double fractions are not exact so I convert to Decimal)

static String NumWords(Decimal n)
{
string[] numbersArr = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
string[] tensArr = new string[] { "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty" };
string[] suffixesArr = new string[] { "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion", "Undecillion", "Duodecillion", "tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion", "Novemdecillion", "Vigintillion" };
string words = "";
var dec = Math.Abs(n % 1);

bool tens = false;

if (n < 0)
{
words += "Negative ";
n *= -1;
}

int power = (suffixesArr.Length + 1) * 3;

while (power > 3)
{
Decimal pow = Convert.ToDecimal(Math.Pow(10, power));
if (n >= pow)
{
if (n % pow > 0)
{
words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
}
else if (n % pow == 0)
{
words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
}
n %= pow;
}
power -= 3;
}
if (n >= 1000)
{
if (n % 1000 >= 1) words += NumWords(Math.Floor(n / 1000)) + " Thousand, ";
else words += NumWords(Math.Floor(n / 1000)) + " Thousand";
n %= 1000;
}
if (0 1)
{
if (words != "")
words += " ";
words += tensArr[(int)n / 10 - 2];
tens = true;
n %= 10;
}

if (n < 20 && n >= 1)
{
if (words != "" && tens == false)
words += " ";
words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
n -= Math.Floor(n);
}
}

if (dec > 0)
{
words += " Point";
while (dec > 0)
{
dec *= 10;
words += " " + NumWords(Math.Floor(dec));
dec = dec % 1;
}
}
if (words == "") words = "Zero";
return words;
}
Posted by Brian on
Also, I fixed a bug where if you, for example, try to use 100000.5 it would crash for index out of range.
Posted by joviermark on

http://csharp.net-informations.com/overview/csharp-console-application.htm

Mark
Posted by Raff on
Thank you Very Much , this is so helpful
Posted by MRezaK on
Thanks so much.
Posted by sumith on
how to code textBox in visual c# windows application form,,,,,,????
Posted by JasonJOUPS on
study doused this [url=http://onlinecasinos-x.com/]online casinos[/url] where you can conquer legal money with the avoid of this lightPlaying [url=http://onlinecasinos-x.com/online-roulette.html]roulette online[/url] , buy [url=http://adultsrus.us]sex toys[/url]
Posted by Robertrom on
Kylie and krew, which includes Jaden Smith and Hailey Baldwin, soaked up the weekend sun with a huge pool get together that has us feeling some variety of way. Ray J featuring Kim Kardashian sex tape [url=http://wordplanes2.skyrock.com/3276040374-Kanye-West-Terrified-Above-Sex-Tape-Leak-gossip.html]can find over here[/url]. What was probably meant to be an artsy snapshot was greeted by jeers instead. "It is a genuine shame and a really exciting story with a lot of bad guys," @cherchezlechic commented. "The only victims here are the Cuban people who are left in their island due to the fact the ones with energy that could assist overturn the dictator chose to run away and abandon them.
Posted by JasonJOUPS on
check doused this [url=http://onlinecasino-b.com/]casino[/url] where you can collect legitimate money with the avoid of this lightPlaying [url=http://onlinecasinos-x.com/online-roulette.html]roulette online[/url] , check this [url=http://onlinecasino-b.com/Nederland/]casino[/url]
Posted by KenndiornhoN on
Propecia Generic Fda Propecia Farligt For Kvinnor Cooper Pharma Viagra Order Celexa Online Canada Amoxicillin Dose Strep Throat [url=http://byuvaigranonile.com]viagra[/url] Cialis E Priapismo

(Your email will not be publicly displayed.)

• 2015
• December
• July
• 2014
• September
• August
• July
• June
• May
• April
• January