How can I Convert Numbers into Albhabets ?

Code for Converting Number 123 as One Hundred Twenty Three:-

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 < 20) str += ones[y]; else str += tens[(int)(y / 10)] + ones[y % 10]; // add triplet modifier only if there // is some output to be modified... if (str != "") 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));
str = str + " Only";
return str;

}

Example:-

Label1.Text = convertNum(Convert.ToInt32(TextBox1.Text));

1 comment: