c# number and words

D

Daniel

Hi Everybody,
I am new to C#, and I am looking for library (open source or liberal
license) which enable me to solve the fallowing problem.


I need to convert numbers into words in different languages for
example:

in english:
123 one hundred twenty three

in german:
123 eins hudrer deiund zwanzig

in polish:
123 sto dwadzieścia trzy
and similar in the most common languages:
Italian, German, English, etc.

I have tried to find.... but without results... in php I have found
this kind of lib, but now I write software in C# ;-) Do you have any
suggestions? Do you know this kind of libs?

Look at this lib in php:
http://pear.php.net/package/Numbers_Words/redirected

regards,
Daniel
 
M

Marcel Müller

Hi,

I am new to C#, and I am looking for library (open source or liberal
license) which enable me to solve the fallowing problem.

I need to convert numbers into words in different languages for
example:

in english:
123 one hundred twenty three

in german:
123 eins hudrer deiund zwanzig

This did not hit the nail on the hat. -> "einhunderddreiundzwanzig"
in polish:
123 sto dwadzieścia trzy
and similar in the most common languages:
Italian, German, English, etc.

I have tried to find.... but without results... in php I have found
this kind of lib, but now I write software in C# ;-) Do you have any
suggestions? Do you know this kind of libs?

I am pretty sure that even the PHP lib does only support some selected
languages without major errors. The conversion is not straight forward
in most languages. In English there are only a few exceptions (e.g.
11-19), in German there are more exceptions because additionally the
last two digits of every block of three digits are swapped. In Japanese
you will finally fail, because they use different number words for
different kind of objects (e.g a piece of paper and a bottle). As long
as you do not know, what you are about to count, you cannot translate. I
am pretty sure that there are dozens of languages, where the conversion
is not biunique in all cases. For the number 1 German is also object
dependent.

But how large are the numbers you want to convert?
Do you need fractional numbers?
If you numbers have no more that a few digits, simply create a table
with the names. E.g. a file with one name per line for each language and
the line number index is the number. Up to 10000 this is no serious
problem. If the perl library fits your needs, you can use it to create
the language resource file.


Marcel
 
D

Daniel

Hi,






This did not hit the nail on the hat. -> "einhunderddreiundzwanzig"



I am pretty sure that even the PHP lib does only support some selected
languages without major errors. The conversion is not straight forward
in most languages. In English there are only a few exceptions (e.g.
11-19), in German there are more exceptions because additionally the
last two digits of every block of three digits are swapped. In Japanese
you will finally fail, because they use different number words for
different kind of objects (e.g a piece of paper and a bottle). As long
as you do not know, what you are about to count, you cannot translate. I
am pretty sure that there are dozens of languages, where the conversion
is not biunique in all cases. For the number 1 German is also object
dependent.

But how large are the numbers you want to convert?
Do you need fractional numbers?
If you numbers have no more that a few digits, simply create a table
with the names. E.g. a file with one name per line for each language and
the line number index is the number. Up to 10000 this is no serious
problem. If the perl library fits your needs, you can use it to create
the language resource file.

Marcel

No only integer values. from typical range for example 0 ... 1 000 000
000 maybe even less....
 
A

Arne Vajhøj

I am new to C#, and I am looking for library (open source or liberal
license) which enable me to solve the fallowing problem.

I need to convert numbers into words in different languages for
example:

in english:
123 one hundred twenty three

in german:
123 eins hudrer deiund zwanzig

in polish:
123 sto dwadzieścia trzy
and similar in the most common languages:
Italian, German, English, etc.

I have tried to find.... but without results... in php I have found
this kind of lib, but now I write software in C# ;-) Do you have any
suggestions? Do you know this kind of libs?

Look at this lib in php:
http://pear.php.net/package/Numbers_Words/redirected

I have never heard about such a library.

I looked at the code and I do not like their code
style.

I think you should start from scratch.

Below is some code to get you started.

It is only tested with English and Danish.

(my German and French is too rusty for me to try
doing those)

So I can not guarantee that you will not hit
problems in other languages.

Arne

==============

using System;

namespace E
{
public abstract class BaseNumberWords
{
protected abstract string GetOne(int v, int gender);
protected abstract string GetTeen(int v);
protected abstract string GetTens(int v);
protected abstract string CombineTenOne(string ten, string one);
protected abstract string CombineHundred(string hundred);
protected abstract string CombineHundredRest(string hundred,
string rest);
protected abstract string CombineThousand(string thousand);
protected abstract string CombineThousandRest(string thousand,
string rest);
protected abstract string CombineMillion(string million);
protected abstract string CombineMillionRest(string million,
string rest);
protected abstract int GetGender(int scale);
public string Convert(int v)
{
return Convert(v, -1);
}
public string Convert(int v, int explicitgender)
{
if(1000000 <= v && v < 1000000000)
{
if(v % 1000000 == 0)
{
return CombineMillion(Convert(v / 1000000,
GetGender(1000000)));
}
else
{
return CombineMillionRest(Convert(v / 1000000,
GetGender(1000000)), Convert(v % 1000000));
}
}
else if(1000 <= v && v < 1000000)
{
if(v % 1000 == 0)
{
return CombineThousand(Convert(v / 1000,
GetGender(1000)));
}
else
{
return CombineThousandRest(Convert(v / 1000,
GetGender(1000)), Convert(v % 1000));
}
}
else if(100 <= v && v < 1000)
{
if(v % 100 == 0)
{
return CombineHundred(GetOne(v / 100, GetGender(100)));
}
else
{
return CombineHundredRest(GetOne(v / 100,
GetGender(100)), Convert(v % 100));
}
}
else if(20 <= v && v < 100)
{
if(v % 10 == 0)
{
return GetTens(v / 10);
}
else
{
return CombineTenOne(GetTens(v / 10), GetOne(v %
10, GetGender(1)));
}
}
else if(10 <= v && v < 20)
{
return GetTeen(v - 10);
}
else if(0 <= v && v < 10)
{
if(explicitgender >= 0)
{
return GetOne(v, explicitgender);
}
else
{
return GetOne(v, GetGender(1));
}
}
else
{
throw new ArgumentException(v + " is not a valid value");
}
}
}
public class EnglishNumberWords : BaseNumberWords
{
private readonly string[][] one = {
new string[] { "zero",
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }
// single gender
};
private readonly string[] teen = { "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eigtheen",
"nineteen" };
private readonly string[] tens = { "", "", "twenty", "thirty",
"fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
protected override string GetOne(int v, int gender)
{
return one[gender][v];
}
protected override string GetTeen(int v)
{
return teen[v];
}
protected override string GetTens(int v)
{
return tens[v];
}
protected override string CombineTenOne(string ten, string one)
{
return String.Format("{0} {1}", ten, one);
}
protected override string CombineHundred(string hundred)
{
return String.Format("{0} hundred", hundred);
}
protected override string CombineHundredRest(string hundred,
string rest)
{
return String.Format("{0} hundred {1}", hundred, rest);
}
protected override string CombineThousand(string thousand)
{
return String.Format("{0} thousand", thousand);
}
protected override string CombineThousandRest(string thousand,
string rest)
{
return String.Format("{0} thousand {1}", thousand, rest);
}
protected override string CombineMillion(string million)
{
return String.Format("{0} million", million);
}
protected override string CombineMillionRest(string million,
string rest)
{
return String.Format("{0} million {1}", million, rest);
}
protected override int GetGender(int scale)
{
return 0; // single gender
}
}
public class DanishNumberWords : BaseNumberWords
{
private readonly string[][] one = {
new string[] { "nul",
"en", "to", "tre", "fire", "fem", "seks", "syv", "otte", "ni" }, //
utrum (common)
new string[] { "nul",
"et", "to", "tre", "fire", "fem", "seks", "syv", "otte", "ni" } //
neutrum (neuter)
};
private readonly string[] teen = { "ti", "elleve", "tolv",
"tretten", "fjorten", "femten", "seksten", "sytten", "atten", "nitten" };
private readonly string[] tens = { "", "", "tyve", "tredive",
"fyrre", "halvtreds", "treds", "halvfjers", "firs", "halvfems" };
protected override string GetOne(int v, int gender)
{
return one[gender][v];
}
protected override string GetTeen(int v)
{
return teen[v];
}
protected override string GetTens(int v)
{
return tens[v];
}
protected override string CombineTenOne(string ten, string one)
{
return String.Format("{1} og {0}", ten, one);
}
protected override string CombineHundred(string hundred)
{
return String.Format("{0} hundrede", hundred);
}
protected override string CombineHundredRest(string hundred,
string rest)
{
return String.Format("{0} hundrede og {1}", hundred, rest);
}
protected override string CombineThousand(string thousand)
{
return String.Format("{0} tusinde", thousand);
}
protected override string CombineThousandRest(string thousand,
string rest)
{
return String.Format("{0} tusinde og {1}", thousand, rest);
}
protected override string CombineMillion(string million)
{
return String.Format("{0} million", million);
}
protected override string CombineMillionRest(string million,
string rest)
{
return String.Format("{0} million og {1}", million, rest);
}
protected override int GetGender(int scale)
{
switch(scale)
{
case 1:
case 1000000:
return 0; // utrum (common)
case 100:
case 1000:
return 1; // neutrum (neuter)
default:
throw new ArgumentException(scale + " is not a
valid scale");
}
}
}
public class Program
{
private static readonly BaseNumberWords en = new
EnglishNumberWords();
private static readonly BaseNumberWords da = new
DanishNumberWords();
public static void Test(int v)
{
Console.WriteLine(v + ":");
Console.WriteLine(" EN = " + en.Convert(v));
Console.WriteLine(" DA = " + da.Convert(v));
}
public static void Main(string[] args)
{
for(int i = 0; i < 100; i++)
{
Test(i);
}
for(int i = 0; i < 50; i++)
{
Test(i*100 + 99);
Test(i*100 + 100);
Test(i*100 + 101);
}
Random rng = new Random();
for(int i = 0; i < 100; i++)
{
Test(rng.Next(0, 1000000000));
}
Console.ReadKey();
}
}
}
 
J

Jeff Johnson

In US English, that is correct. In UK English the correct answer is
"one hundred and twenty three". There is an "and" in the UK version.

I just saw a similar discussion the other day, and after one Brit brought up
the point you just made, another replied (and I don't remember the exact
wording) that there is more than one convention for numbers in BrE and they
are applied at specific times, so in some cases "one hundred twenty-three"
is correct.

Also, I'm pretty sure that in both flavors of English a hyphen is required
between the tens and ones places: "twenty-three" not "twenty three."
 
A

Arne Vajhøj

I just saw a similar discussion the other day, and after one Brit brought up
the point you just made, another replied (and I don't remember the exact
wording) that there is more than one convention for numbers in BrE and they
are applied at specific times, so in some cases "one hundred twenty-three"
is correct.

Also, I'm pretty sure that in both flavors of English a hyphen is required
between the tens and ones places: "twenty-three" not "twenty three."

It should not be difficult to be able to handle different flavors of
English.

The original PHP code did.

Any the C# code I posted did as well.

Arne
 

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