Welcome to WebmasterWorld Guest from 18.206.48.142

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help with script

Convert numbers into words, and translate

     
6:44 pm on Feb 24, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts: 2175
votes: 0


A bit like when you write the value on a cheque in whole words, I need a script to take a value of a basket and convert to words on the email it sends, it has to be in French too on some orders.

e.g
Input = 20611361 fcfa
Output = vingt millions six cent onze mille trois cent sixante et un francs

Any ideas ?
7:41 pm on Feb 24, 2018 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


This topic might help : [openclassrooms.com...]
8:18 am on Feb 25, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts: 2175
votes: 0


ahh thanks but my French isn't good. I was thinking the french translation would be the easy bit, just using a select case with 30 entries of translations for numbers 1-20, 30-90, hundred, thousand, million & billion, and then send it an array of word in English. What I'm struggling with is how to convert a number into words
5:05 pm on Feb 25, 2018 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


Sorry, I thought you were french.

You can install the "Internationalization extension" : [php.net...]

I don't have this extension installed myself, so I can't test it fully, but then all you need to do is :

$NF = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $NF->format(20611361);


so I guess you can replace the "en" by "fr" too. But again, I can't test it myself.

PS: I installed the intl extension to check and yes it does what you want. If you put "fr" it will spell the number in French.
5:47 pm on Feb 25, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2690
votes: 815


I don't code in PHP but I can explain how I would do it in general. (I would typically do this in Python or JS)
Split the string into block of three digits from the right.
20 611 361
Then split the blocks of three digits into two blocks one block for the units and tens and the other for the hundreds.
"61" "3" , "11" "6", "20"
Then create a lookup table for the first block listing each number from 1 to 99 as the key and the value the French written form.
{1:'un',
2:'deux',
3:'trois''
...
99:' 'quatre-vingt dix neuf'}

then a create second table for the hundreds:
{1:'cent',
2:'deux cents',
3: trois cents'
...}

Now are you ready to start converting.
Take the units and tens numbers, look them up.
61 = 'soixante et un'
then look up the hundreds
3 = 'trois cent'

Then block two:
11 = onze
6 = six cent

block three
20 = vingt

Then stick the blocks together while including the thousands and millions as required
From left to right:
block one => 'vingt' append ' millions'
block two => 'six cent' + ' ' + 'onze' append ' milles'
block three => 'trois cent' + ' ' + 'soixante et un'

result = 'vingt millions six cent onze milles trois cent soixante et un'

You will need to make a refinement for the plural and singular forms of the thousands and millions. So if it one million, you do not show one millions.

Now the specifics on how to code this PHP is up to you or if someone else here who has more time and experience, but this is the patter I would use if I were coding this in Python.

Note: you could further refine the code by splitting the tens and units, but I feel there are too many exceptions. So it might be time consuming and not very elegant to create a 100 element long look up table but managing all the exception would be far more complex and annoying in the end.
5:56 pm on Feb 25, 2018 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


NickMNS example is good, but it looks like it's not taking in consideration the french grammar (some words take a "s" or "not" depending of the following word, etc...). The PHP intl extension does, including hyphens. I didn't know about this extension, until I saw the post of aspdaddy , we can learn things everyday :-)
6:36 pm on Feb 25, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2690
votes: 815


Pluralization in this case is only a concern for millions and thousands and it is works as in English. All other case where pluralization would be a concern should be covered by the look up table. That is the correct form should already be present in the table and should not change as result of other numbers in the string.

I am fluent in French, I live in Montreal (French Canada) so as far as I know the output should be correct, but I haven't tested it so there may well be edge cases that i am not thinking of. As for hyphens, there should be no hyphens required.

As to the workings of PHP I have very little to no-experience.
7:25 pm on Feb 25, 2018 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


Okay !
5:36 pm on Feb 26, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts: 2175
votes: 0


I got it thanks ,all working mostly OK in English

Just a bit of tweaking to add the word "and" in a couple places and make the interface work for a few different major/minor currency and 2 languages.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members