Mohamed Shakir - 1 year ago 77
C# Question

# converting all numbers including cents to word

My code only converts the full numbers within (1-9999). I need to convert all numbers and if a number(e.g : 2564866258) contain cents(e.g : 1928.25) to be converted to words. Below is my code. Can anyone help me to solve this problem.

``````private void amt_txt_KeyUp(object sender, KeyEventArgs e)
{
string[] Ones = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen" };

string[] Tens = { "Ten", "Twenty", "Thirty", "Fourty", "Fift", "Sixty", "Seventy", "Eighty", "Ninty" };

int no = int.Parse(amt_txt.Text);
string strWords = "";

if (no > 999 && no < 10000)
{
int i = no / 1000;
strWords = strWords + Ones[i - 1] + " Thousand ";
no = no % 1000;
}

if (no > 99 && no < 1000)
{
int i = no / 100;
strWords = strWords + Ones[i - 1] + " Hundred ";
no = no % 100;
}

if (no > 19 && no < 100)
{
int i = no / 10;
strWords = strWords + Tens[i - 1] + " ";
no = no % 10;
}

if (no > 0 && no < 20)
{
strWords = strWords + Ones[no - 1];
}

cnv_txt.Text = strWords;

}
``````

This is the Converter that you need. Using methods reduce complexity of your code.

this algorithm convert number to words like this.

Input : "1234567"

1. Get the 3 last digits. "567"

2. Convert it to Words. Five Hundred Sixty Seven

3. Apply its separator. Five Hundred Sixty Seven (There is no separator for 3 last digits)

4. Repeat: Get the 3 last digits "234"

5. Convert it to Words. Two Hundred Thirty Four

6. Apply its separator. Two Hundred Thirty Four Thousand

7. Append to Resault.Two Hundred Thirty Four Thousand Five Hundred Sixty Seven

8. Repeat : Get the 3 last digits. "1" (Only one digit left)

9. Convert it to Words. One

10. Apply its separator. One Million

11. Append to Resault.One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven

Done.

I have Commented the code hope it helps.

If you didnt understand some parts just ask ill explain.

Also for Decimals we separate them, convert them to words. finally we add add them to result at the end.

``````    private static void Main(string[] args)
{

string input = "123466265.123";

// take decimal part of input. convert it to word. add it at the end of method.
string decimals = "";

if (input.Contains("."))
{
decimals = input.Substring(input.IndexOf(".") + 1);
// remove decimal part from input
input = input.Remove(input.IndexOf("."));
}

// Convert input into words. save it into strWords
string strWords = GetWords(input);

if (decimals.Length > 0)
{
// if there is any decimal part convert it to words and add it to strWords.
strWords += " Point " + GetWords(decimals);
}

Console.WriteLine(strWords);
}

private static string GetWords(string input)
{
// these are seperators for each 3 digit in numbers. you can add more if you want convert beigger numbers.
string[] seperators = { "", " Thousand ", " Million ", " Billion " };

// Counter is indexer for seperators. each 3 digit converted this will count.
int i = 0;

string strWords = "";

while (input.Length > 0)
{
// get the 3 last numbers from input and store it. if there is not 3 numbers just use take it.
string _3digits = input.Length < 3 ? input : input.Substring(input.Length - 3);
// remove the 3 last digits from input. if there is not 3 numbers just remove it.
input = input.Length < 3 ? "" : input.Remove(input.Length - 3);

int no = int.Parse(_3digits);
// Convert 3 digit number into words.
_3digits = GetWord(no);

// apply the seperator.
_3digits += seperators[i];
// since we are getting numbers from right to left then we must append resault to strWords like this.
strWords = _3digits + strWords;

// 3 digits converted. count and go for next 3 digits
i++;
}
return strWords;
}

// your method just to convert 3digit number into words.
private static string GetWord(int no)
{
string[] Ones =
{
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen"
};

string[] Tens = {"Ten", "Twenty", "Thirty", "Fourty", "Fift", "Sixty", "Seventy", "Eighty", "Ninty"};

string word = "";

if (no > 99 && no < 1000)
{
int i = no/100;
word = word + Ones[i - 1] + " Hundred ";
no = no%100;
}

if (no > 19 && no < 100)
{
int i = no/10;
word = word + Tens[i - 1] + " ";
no = no%10;
}

if (no > 0 && no < 20)
{
word = word + Ones[no - 1];
}

return word;
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download