Converting given no. to words

G

Guest

Hi,
I have little experience in C#, Can anyone help me with this sproblem. " I want to convert a given number in a text to a word on a lable" for eg if we type 25 in text box , i want to display it as 'Twenty Five' on a lable. pls send any related code sample.
 
D

Daniel Bass

I'm not sure .Net has any library that will allow you to do this
automatically...

One thing you could do is evaluate the number expression by unit, then
ten's, then hundreds, etc...

So for "54321", your algorithm would be hard coded to convert 1 to "One",
then the 2 in the tens column to "Twenty", the 3 to "Three Hundred and", the
4 to "Four Thousand", the 5 to "Fifty"...
You could simply prepend you new result to a string your building, ending up
with (reading backwards) Fifty Four Thousan Three Hundread and Twenty One...

Each conversion taking place because of the single digit it is, and the unit
it exists as...

Obviously there are things you need to watch out for, like knowing that you
need to group both the first and second digit if the tens digit is 1. For
example, 15 wouldn't work because it would be "Ten Five", but "Fifteen"...

Good luck.

Converting Numeric to a word said:
Hi,
I have little experience in C#, Can anyone help me with this sproblem. " I
want to convert a given number in a text to a word on a lable" for eg if we
type 25 in text box , i want to display it as 'Twenty Five' on a lable. pls
send any related code sample.
 
N

Nicholas Paldino [.NET/C# MVP]

You would have to do this manually. There is nothing in the framework
that would help you with this out of the box.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Converting Numeric to a word said:
Hi,
I have little experience in C#, Can anyone help me with this sproblem. " I
want to convert a given number in a text to a word on a lable" for eg if we
type 25 in text box , i want to display it as 'Twenty Five' on a lable. pls
send any related code sample.
 
M

Morten Wennevik

Hi,

You need to consider the number in triplets and treat them separatly while adding a thousand or million when needed.

Anyway, I was bored so I decided to try to create such a feature :p
This method will take a number, split it into millions, thousands and the rest
andflag is used to prevent getting "and" as the first number in some cases. You might want to consider using a class global string or reference the string. If you want numbers above 999 million, you need to switch to long.

string GetStringFromNumber(int n)
{
string number = n.ToString();
string s = "";

bool andflag = false;
if(number.Length > 6)
{
s += DoTriplet((n%1000000000) / 1000000, andflag) + " million ";
andflag = true;
}
if(number.Length > 3)
{
s += DoTriplet((n%1000000) / 1000, andflag) + " thousand ";
andflag = true;
}
else
andflag = false;
s += DoTriplet(n % 1000, andflag);

return s;
}

This method will create hundreds, tens and ones and assemble a string

string DoTriplet(int n, bool andflag)
{
int o = n%10;
int t = (n%100/10);
int h = n/100;

string s = "";
if(h != 0)
{
s += GetOnes(h) + " hundred ";
}
if(t != 0 || o != 0)
{
if(h != 0 || andflag)
s += "and ";
}
if(t == 1 && o != 0)
{
s += GetFirst(t*10 + o);
}
else
{
s += GetTens(t) + " " + GetOnes(o);
}
return s;
}

Then follows a series of switch methods to fill in the values

string GetOnes(int n)
{
switch(n)
{
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
case 4:
return "four";
case 5:
return "five";
case 6:
return "six";
case 7:
return "seven";
case 8:
return "eight";
case 9:
return "nine";
default:
return "";
}
}

string GetTens(int n)
{
switch(n)
{
case 1:
return "ten";
case 2:
return "twenty";
case 3:
return "thirty";
case 4:
return "fourty";
case 5:
return "fifty";
case 6:
return "sixty";
case 7:
return "seventy";
case 8:
return "eighty";
case 9:
return "ninety";
default:
return "";
}
}

string GetFirst(int n)
{
switch(n)
{
case 11:
return "eleven";
case 12:
return "twelve";
case 13:
return "thirteen";
case 14:
return "fourteen";
case 15:
return "fifteen";
case 16:
return "sixteen";
case 17:
return "seventeen";
case 18:
return "eighteen";
case 19:
return "nineteen";
default:
return "";
}
}

I sure hope this wasn't homework :p
 
M

Mohamoss

hi
i don't know of buit in function to do that but one simple way to do it is
to use the mod function , then it really depends on the lenth of the number
..
like to get the units use the number -the number mod 10, for tens use mod
100
forexample the units of 25 can num- (num-(num%10) )-->
25-(25-(25%10) )
hope that helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
J

Justin Rogers

I guess I'll toss my algorithm in as well, since I couldn't pass up such a fun
programming
task, even in the wee hours of the morning when I should be sleeping. I went
ahead and
posted on my blog for longevity, and in the interest of eventually providing a
version that
works on decimal numbers as well.

http://weblogs.asp.net/justin_rogers/archive/2004/06/09/151675.aspx


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Morten Wennevik said:
Hi,

You need to consider the number in triplets and treat them separatly while
adding a thousand or million when needed.
Anyway, I was bored so I decided to try to create such a feature :p
This method will take a number, split it into millions, thousands and the rest
andflag is used to prevent getting "and" as the first number in some cases.
You might want to consider using a class global string or reference the string.
If you want numbers above 999 million, you need to switch to long.
 
D

Daniel Bass

Yes you guys really were bored! ;)


Justin Rogers said:
I guess I'll toss my algorithm in as well, since I couldn't pass up such a fun
programming
task, even in the wee hours of the morning when I should be sleeping. I went
ahead and
posted on my blog for longevity, and in the interest of eventually providing a
version that
works on decimal numbers as well.

http://weblogs.asp.net/justin_rogers/archive/2004/06/09/151675.aspx


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Morten Wennevik said:
Hi,

You need to consider the number in triplets and treat them separatly
while
adding a thousand or million when needed.
Anyway, I was bored so I decided to try to create such a feature :p
This method will take a number, split it into millions, thousands and the rest
andflag is used to prevent getting "and" as the first number in some
cases.
You might want to consider using a class global string or reference the string.
If you want numbers above 999 million, you need to switch to long.
 
J

Justin Rogers

Bored? No, this is bored:

private static string[] groupMapping =
new string[] {
"Hundred", "Thousand", "Million", "Billion", "Trillion",
"Quadrillion", "Quintillion", "Sextillian",
"Septillion", "Octillion", "Nonillion", "Decillion", "Undecillion",
"Duodecillion", "Tredecillion",
"Quattuordecillion", "Quindecillion", "Sexdecillion",
"Septendecillion", "Octodecillion", "Novemdecillion",
"Vigintillion", "Unvigintillion", "Duovigintillion", "10^72",
"10^75", "10^78", "10^81", "10^84", "10^87",
"Vigintinonillion", "10^93", "10^96", "Duotrigintillion",
"Trestrigintillion"
};

// NOTE: 10^303 is approaching the limits of double, as ~1.7e308 is
where we are going
// 10^303 is a centillion and a 10^309 is a duocentillion



--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Daniel Bass said:
Yes you guys really were bored! ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top