Tuesday, April 10, 2012

how to display the spelling of a number in asp.net

I have a requirement to display the spelling of the number instead number(0-999) in asp.net.For this i tried so many ways but i did not get the desired result.Finally i will go thorough the simple string array concept which is pretty simple to get the result. Here i will take string arrays which are ones,tens. Based on  the number range i will check the condition and assign the array item to result string
int n=int.parse(txtnumber.Text);
int i=0;
string s="";
string [] ones=new string[]{"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen",fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};

string [] Tens=new string[]{"one","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninty"};

if(n>99&&n<1000)
{
i=n/100;
s=ones[i-1]+"hundred";
n=n%100;
}
if(n>19&&n<1000)
{
i=n/10;
s=s+ten[i-1];
n=n%10;
}
if(n>0&&n<20)
{
s=s+one[n-1];
}
Txtspell.Text=s;

No comments:

Bel