We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,376 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to Convert number from digits to words

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?

5
Contributors
7
Replies
2 Days
Discussion Span
4 Months Ago
Last Updated
8
Views
vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

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

cmps
Junior Poster
117 posts since Nov 2011
Reputation Points: 28
Solved Threads: 17
Skill Endorsements: 2

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.';
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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).

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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.

diafol
Keep Smiling
Moderator
10,666 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,514
Skill Endorsements: 57

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?

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5
Bachov Varghese
Junior Poster
123 posts since Sep 2011
Reputation Points: 41
Solved Threads: 29
Skill Endorsements: 3

@Bachov Varghese

Good!

Trying for marathi.

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0755 seconds using 2.66MB