Monday, January 5, 2009

Converting Numbers To Word Format

Declare global variable like-

string[] ones = { "", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" };
string[] tens = { "", "", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" };
string[] triplets = { "", " Thousand", " Lac", " Ten Lac", " Crore", " Quadrillion", " Quintillion", " Sextillion", " Septillion", " Octillion", " Nonillion" };

Now, make two functions-
public string convertTri(int num, int tri)
{
string str;
int r, x, y;
// chunk the number, ...rxyy
r = (int)(num / 1000);
x = (num / 100) % 10;
y = num % 100;

// init the output string
str = "";

// do hundreds
if (x > 0)
str = ones[x] + " Hundred";

// do ones and tens
if (y <>


str += triplets[tri];

// continue recursing?
if (r > 0)
return convertTri(r, tri + 1) + str;
else
return str;
}

public string convertNum(int num)
{
try
{
num = (int)num; // make sure it's an integer
}
catch
{
num = 0;
}

if (num == 0)
return "Zero";
string str = (convertTri(num, 0));
return str;

}
These r the generalise function. in ur page call ConverNum(250).
& the see the result...

No comments:

Post a Comment