Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need algorithm for converting the numbers in letters, in 4 languages

Status
Not open for further replies.

mondi

Programmer
Sep 10, 2003
169
AL
Hi everybody!
I have to make an algorithm which gets a number and converts it in 4 different languages in letters, I mean e.g. the number 102 to be converted in one hundred-two; ein hundert zwei; cent deux; cento due;

Actually as for the english I can make an algorithm. It will be something like this:

we have our number nr as integer
string number = "";
get an array of strings digits ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", 'ten', 'eleven', 'twelve', 'thirteen','fourteen','fifteen', 'sixteen', 'seventeen','eighteen','nineteen']
get another array of strings: array1 ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]

if (nr < 20)
number = digits[nr]
else
if (nr % 10 == 0)
number = array1[nr / 10 - 2]
else number = array1[nr / 10 - 2] + "-" + digits[nr % 10]

this idea goes then for numbers that are bigger or equal to one hundred.
My problem is that writing this for every language seems not like a good idea, but I couldn't find another way to do it. Can anybody help me?
P.S. sorry for the mistakes in coding, but I was in hurry and some of them, I made with copy paste.
Any help would be very appreciated.
Thanks
 
How's your Fortran? Look for NumToWords.f in
It does the German, English, Chinese and Roman Numerals but not French and whatever the other language is.
 
With my design head on, the bad news is that you should to treat them all differently. You need a strategy pattern, which means according to the Gang of Four, you
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.
They have to be separate algorithms because they have different rules. For example, French has a different rule to English for 101-109 so one hundred and five, cent cinq. Eighty is quatre vingts (plural), but 85 is quatre vingt cinq. English: one hundred and forty-two. American: One hundred forty-two.

By keeping them apart, you can easily tailor them exactly to the language in question, without worrying about silly mistakes of the "you must change your password in the next 1 days" variety. It also makes it easier to cope when your company opens an office in Vladivostok and you need to do it in Cyrillic...

 
LIke stevexff says -- you need to isolate each locales behavior because they act differently.

The algorithm, btw, is a common assignment in 1st semester computer science. So a Google should turn up all sorts of hits on it.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thank you guys for your answers. I think stevexff is right. That's how I'll do it. I know it's not difficult to do it for each language, but for four languages I thought that perhaps, I had to implement the same idea four times. That's why I thought for a common algorithm but I see that I would mess up everything doing it like this.
Happy New Year for you all
 
Watch out for the hyphenation and extra s or et in some numbers in French.
 
Yes thanks xwb. But can anyone tell me about any site that has the numbers in french maybe, or english or american officially written
 
Try It has the rules up to 1000 including the extra s and et.

As I said earlier, just translate numtowords.f for German and English. It is not that difficult: I wrote it in C originally and translated it to Fortran. All you have to do is the reverse. Unfortunately, I've lost the original C source. The numbers are in UK English: not US English. i.e. 1200 is one thousand two hundred: not twelve hundred.
 

Hi Mondi

My Suggestions

Try Using XML files to store those values (.Config)
and using XML reader instead of hardcoding those values.

If you implement this then it very easy to add many more languages.

 
Well, but I can't see the goodness of storing the values in XML. How could it make it easier to store the values in XML, and then using XML reader?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top