Thursday, March 24, 2011

Convert number to words.

Add button and textbox on the design page and on click event of the button on the coding page write this code.

private void button1_Click(object sender, EventArgs e)
{
Class2 obj = new Class2();



MessageBox.Show(obj.NumberToWords(Convert.ToInt32(textBox1.Text)) +" ruppees);
}

Create Class with the same name and call the function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication2
{
class Class2
{
private string[] _smallNumbers = new string[]
{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen"};

// Tens number names from twenty upwards
private string[] _tens = new string[]
{"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty",
"Ninety"};

// Scale number names for use during recombination
private string[] _scaleNumbers = new string[]
{"", "Thousand", "Million", "Billion"};



public String NumberToWords(int number)
{
if (number == 0)
{
return _smallNumbers[0];
}
int[] digitGroups = new int[4];

// Ensure a positive number to extract from
int positive = Math.Abs(number);

// Extract the three-digit groups
for (int i = 0; i < grouptext =" new" i =" 0;" combined =" groupText[0];" appendand =" (digitGroups[0]"> 0) && (digitGroups[0] < i =" 1;" prefix =" groupText[i]" appendand =" false;" combined =" prefix" combined = "Negative " grouptext = "" hundreds =" threeDigits" tensunits =" threeDigits" tens =" tensUnits" units =" tensUnits">= 2)
{
groupText += _tens[tens];
if (units != 0)
{
groupText += " " + _smallNumbers[units];
}
}
else if (tensUnits != 0)
groupText += _smallNumbers[tensUnits];

return groupText;
}

}
}

No comments :

Post a Comment