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();
}
}
}