Number to text conversion


Posted in: data | Save to del.icio.us | Twit This! 25 Apr 2009

phpguru.org offers a small and useful class to convert numbers to their textual equivalent. If you are building a accounting or invoicing application it may come in handy. The class can be downloaded form here. A sample code using the class is shown below.

<?php
 
    require_once 'libs/numtotext.php';
 
 
    $tt = new TextualNumber();
 
    /* Catch 'out of range' and 'invalid character' exceptions */
    try {
        echo $tt->GetText("197533408989");
    }
    catch(Exception $e) {
        echo $e->getMessage();
    }
 
?>

The above code prints:

one hundred and ninety-seven billion 
five hundred and thirty-three million 
four hundred and eight thousand 
nine hundred and eighty-nine

The class supports a range up to 999999999999999999 and also supports decimal numbers.
One thing to note is that The billion/trillion etc suffixes are done using the American style (eg 9 zeros for billion, 12 for trillion).




Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts



2 Responses

1

Shantanu Oak

May 13th, 2009 at 9:36 pm

>> One thing to note is that The billion/trillion etc suffixes are done using the American style

If you want British style, use Pear package called “Numbers Words”

http://pear.php.net/package/Numbers_Words

2

roy

September 2nd, 2009 at 8:29 pm

nice, thanx, but i suggest that would better if you delete the “and ” in hundred

// Three digit number
case ‘3′:
if ($int % 100 == 0) {
$text = self::$units[$int{0}] . ‘ hundred’;
} else {
$text = self::$units[$int{0}] . ‘ hundred and ‘ . self::GetText(substr($int, 1));
}
break;

example:

197533408989.89

result:

One hundred ninety-seven billion,
five hundred thirty-three million,
four hundred eight thousand,
nine hundred eighty-nine and 89/100

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]

  • Users Online

    • 15 Users Online
    • 12 Guests, 3 Bots