1,105,625 Community Members

How to Convert number from digits to words

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

How to Convert number from digits to words into two languages English language and Marathi language
Is it possible using php?

Can anybody help me?

Member Avatar
cmps
Posting Whiz in Training
226 posts since Nov 2011
Reputation Points: 26 [?]
Q&As Helped to Solve: 32 [?]
Skill Endorsements: 3 [?]
 
0
 

Dear vizz, check this link for the full ways to convert integer to string in php
http://stackoverflow.com/questions/1035634/converting-an-integer-to-a-string-in-php

Member Avatar
broj1
Posting Virtuoso
1,578 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

If I understand the question correctly you would like to suplement numbers with words in two languages. I would do that with arrays. If the range of numbers is not too big you can map numbers to words directly:

$englishWords = array(
    1 => 'one',
    2 => 'two',
    3 => 'three',
    1 => 'four',
    5 => 'five',
)

$marathiWords = array(
    1 => 'one in marathi',
    2 => 'two in marathi',
    3 => 'three in marathi',
)

echo '2 is ' . $englishWords[2] . ' in english and ' . $marathiWords[2] . ' in marathi.';
Member Avatar
broj1
Posting Virtuoso
1,578 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

Depending on how you intend to use the numbers they could be also in one two-dimensional array:

// using italian in this example since I do not speak marathi :-)
$number2words = array(
    1 => array('english' => 'one', 'italian' => 'uno'),
    2 => array('english' => 'two', 'italian' => 'due'),
    3 => array('english' => 'three', 'italian' => 'tre'),
);

echo '2 is ' . $number2words[2]['english'] . ' in english and ' . $number2words[2]['italian'] . ' in italian.';

If you have a big range of numbers then you have to make up some more clever logic to display numbers with words (like interpreting hundreds, thousand etc).

Member Avatar
diafol
Where are my eyes?
12,985 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
1
 

This can be more complicated in non-English languages. Most languages have distinct names for 0-20, and mutiples of 10 up to 100 - some of which seem to be multiples of others, e.g. 80 in French = "quatre-vingts". Then distincts for the occasional power of 10: (100, 1000, 1000000 ...)

Some, like Italian contract when two consecutive vowels come into contact, e.g.

21: ventuno (venti + uno)
38: trentotto (trenta + otto)

A further complication can arise if using gender forms for certain objects, e.g. Welsh

2: dau, dwy
3: tri, tair

These issues tend not to arise in English, but sometimes you need to insert things like "and", e.g. one hundred "and" one; two thousand "and" forty-six.

Marathi from what I can gather has a similar pattern to most others?

Perhaps the best approach would be to create a 'rules' array or 'combination' array for each language too.

Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

So, I have to write each number into word in array , am I right?

Is there any function in php to convert number into word automatically?

Member Avatar
Bachov Varghese
Posting Whiz in Training
205 posts since Sep 2011
Reputation Points: 45 [?]
Q&As Helped to Solve: 52 [?]
Skill Endorsements: 6 [?]
 
2
 
Member Avatar
vizz
Posting Pro in Training
427 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 9 [?]
 
0
 

@Bachov Varghese

Good!

Trying for marathi.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article