home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Numbers / Words / lang.en_100.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  8.6 KB  |  308 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2003 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 3.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/3_0.txt.                                  |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Piotr Klaban <makler@man.torun.pl>                          |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: lang.en_100.php,v 1.2 2004/09/02 11:07:59 makler Exp $
  21. //
  22. // Numbers_Words class extension to spell numbers in Donald Knuth system, in English language.
  23. //
  24.  
  25. /**
  26.  * Class for translating numbers into Donald Knuth system, in English language.
  27.  *
  28.  * @author Piotr Klaban
  29.  * @package Numbers_Words
  30.  */
  31.  
  32. /**
  33.  * Include needed files
  34.  */
  35. require_once("Numbers/Words.php");
  36.  
  37. /**
  38.  * Class for translating numbers into Donald Knuth system, in English language.
  39.  *
  40.  * @author Piotr Klaban
  41.  * @package Numbers_Words
  42.  */
  43. class Numbers_Words_en_100 extends Numbers_Words
  44. {
  45.  
  46.     // {{{ properties
  47.     
  48.     /**
  49.      * Locale name
  50.      * @var string
  51.      * @access public
  52.      */
  53.     var $locale      = 'en_100';
  54.  
  55.     /**
  56.      * Language name in English
  57.      * @var string
  58.      * @access public
  59.      */
  60.     var $lang        = 'English (Donald Knuth system)';
  61.  
  62.     /**
  63.      * Native language name
  64.      * @var string
  65.      * @access public
  66.      */
  67.     var $lang_native = 'English (Donald Knuth system)';
  68.     
  69.     /**
  70.      * The word for the minus sign
  71.      * @var string
  72.      * @access private
  73.      */
  74.     var $_minus = 'minus'; // minus sign
  75.     
  76.     /**
  77.      * The sufixes for exponents (singular and plural)
  78.      * Names based on:
  79.      * http://home.earthlink.net/~mrob/pub/math/largenum.html
  80.      * Donald Knuth system (power of 2)
  81.      * @var array
  82.      * @access private
  83.      */
  84.     var $_exponent = array(
  85.         0 => array(''),
  86.         2 => array('hundred'),
  87.         4 => array('myriad'),
  88.         8 => array('myllion'),
  89.        16 => array('byllion'),
  90.        32 => array('tryllion'),
  91.        64 => array('quadryllion'),
  92.       128 => array('quintyllion'),
  93.       256 => array('sextyllion'),
  94.       512 => array('septyllion'),
  95.      1024 => array('octyllion'),
  96.      2048 => array('nonyllion'),
  97.      4096 => array('decyllion'),
  98.      8192 => array('undecyllion'),
  99.     16384 => array('duodecyllion'),
  100.     32768 => array('tredecyllion'),
  101.     65536 => array('quattuordecyllion'),
  102.    131072 => array('quindecyllion'),
  103.    262144 => array('sexdecyllion'),
  104.    524288 => array('septendecyllion'),
  105.   1048576 => array('octodecyllion'),
  106.   2097152 => array('novemdecyllion'),
  107.   4194304 => array('vigintyllion')
  108.         );
  109.  
  110.     /**
  111.      * The array containing the digits (indexed by the digits themselves).
  112.      * @var array
  113.      * @access private
  114.      */
  115.     var $_digits = array(
  116.         0 => 'zero', 'one', 'two', 'three', 'four',
  117.         'five', 'six', 'seven', 'eight', 'nine'
  118.     );
  119.  
  120.     /**
  121.      * The word separator
  122.      * @var string
  123.      * @access private
  124.      */
  125.     var $_sep = ' ';
  126.     // }}}
  127.     // {{{ toWords()
  128.     /**
  129.      * Converts a number to its word representation
  130.      * in Donald Knuth system, in English language.
  131.      *
  132.      * @param  integer $num   An integer between -infinity and infinity inclusive :)
  133.      *                        that need to be converted to words
  134.      * @param  integer $power The power of ten for the rest of the number to the right.
  135.      *                        Optional, defaults to 0.
  136.      * @param  integer $powsuffix The power name to be added to the end of the return string.
  137.      *                        Used internally. Optional, defaults to ''.
  138.      *
  139.      * @return string  The corresponding word representation
  140.      *
  141.      * @access public
  142.      * @author Piotr Klaban <makler@man.torun.pl>
  143.      * @since  PHP 4.2.3
  144.      */
  145.     function toWords($num, $power = 0, $powsuffix = '') {
  146.       $ret = '';        
  147.       
  148.       // add a minus sign
  149.       if (substr($num, 0, 1) == '-') {
  150.         $ret = $this->_sep . $this->_minus;
  151.         $num = substr($num, 1);
  152.       }
  153.         
  154.       // strip excessive zero signs and spaces
  155.       $num = trim($num);
  156.       $num = preg_replace('/^0+/','',$num);
  157.         
  158.       if (strlen($num) > 3) {
  159.           $maxp = strlen($num)-1;
  160.           $curp = $maxp;
  161.           for ($p = $maxp; $p > 0; --$p) { // power
  162.             
  163.             // check for highest power
  164.             if (isset($this->_exponent[$p])) {
  165.               // send substr from $curp to $p
  166.               $snum = substr($num, $maxp - $curp, $curp - $p + 1);
  167.               $snum = preg_replace('/^0+/','',$snum);
  168.               if ($snum !== '') {
  169.                   $cursuffix = $this->_exponent[$power][count($this->_exponent[$power])-1];
  170.                   if ($powsuffix != '')
  171.                     $cursuffix .= $this->_sep . $powsuffix;
  172.                   $ret .= $this->toWords($snum, $p, ''); // $cursuffix);
  173.                   // normally cursuffix is added at the end, but not here
  174.               }
  175.               $curp = $p - 1;
  176.               continue;
  177.             }
  178.           }
  179.           $num = substr($num, $maxp - $curp, $curp - $p + 1);
  180.           if ($num == 0) {
  181.               return $ret;
  182.           }
  183.       } elseif ($num == 0 || $num == '') {
  184.         return $this->_sep . $this->_digits[0];
  185.       }
  186.     
  187.       $h = $t = $d = 0;
  188.       
  189.       switch(strlen($num)) {
  190.         case 3:
  191.           $h = (int)substr($num,-3,1);
  192.  
  193.         case 2:
  194.           $t = (int)substr($num,-2,1);
  195.  
  196.         case 1:
  197.           $d = (int)substr($num,-1,1);
  198.           break;
  199.  
  200.         case 0:
  201.           return;
  202.           break;
  203.       }
  204.     
  205.       if ($h) {
  206.         $ret .= $this->_sep . $this->_digits[$h] . $this->_sep . 'hundred';
  207.  
  208.         // in English only - add ' and' for [1-9]01..[1-9]99
  209.         // (also for 1001..1099, 10001..10099 but it is harder)
  210.         // for now it is switched off, maybe some language purists
  211.         // can force me to enable it, or to remove it completely
  212.         // if (($t + $d) > 0)
  213.         //   $ret .= $this->_sep . 'and';
  214.       }
  215.  
  216.       // ten, twenty etc.
  217.       switch ($t) {
  218.       case 9:
  219.       case 7:
  220.       case 6:
  221.           $ret .= $this->_sep . $this->_digits[$t] . 'ty';
  222.           break;
  223.     
  224.       case 8:
  225.           $ret .= $this->_sep . 'eighty';
  226.           break;
  227.     
  228.       case 5:
  229.           $ret .= $this->_sep . 'fifty';
  230.           break;
  231.     
  232.       case 4:
  233.           $ret .= $this->_sep . 'forty';
  234.           break;
  235.     
  236.       case 3:
  237.           $ret .= $this->_sep . 'thirty';
  238.           break;
  239.     
  240.       case 2:
  241.           $ret .= $this->_sep . 'twenty';
  242.           break;
  243.     
  244.       case 1:
  245.           switch ($d) {
  246.           case 0:
  247.               $ret .= $this->_sep . 'ten';
  248.               break;
  249.     
  250.           case 1:
  251.               $ret .= $this->_sep . 'eleven';
  252.               break;
  253.     
  254.           case 2:
  255.               $ret .= $this->_sep . 'twelve';
  256.               break;
  257.     
  258.           case 3:
  259.               $ret .= $this->_sep . 'thirteen';
  260.               break;
  261.     
  262.           case 4:
  263.           case 6:
  264.           case 7:
  265.           case 9:
  266.               $ret .= $this->_sep . $this->_digits[$d] . 'teen';
  267.               break;
  268.     
  269.           case 5:
  270.               $ret .= $this->_sep . 'fifteen';
  271.               break;
  272.     
  273.           case 8:
  274.               $ret .= $this->_sep . 'eighteen';
  275.               break;
  276.           }
  277.           break; 
  278.       }
  279.  
  280.       if ($t != 1 && $d > 0) { // add digits only in <0>,<1,9> and <21,inf>
  281.         // add minus sign between [2-9] and digit
  282.         if ($t > 1) {
  283.           $ret .= '-' . $this->_digits[$d];
  284.         } else {
  285.           $ret .= $this->_sep . $this->_digits[$d];
  286.         }
  287.       }
  288.   
  289.       if ($power > 0) {
  290.         if (isset($this->_exponent[$power]))
  291.           $lev = $this->_exponent[$power];
  292.     
  293.         if (!isset($lev) || !is_array($lev))
  294.           return null;
  295.      
  296.         $ret .= $this->_sep . $lev[0];
  297.       }
  298.     
  299.       if ($powsuffix != '')
  300.         $ret .= $this->_sep . $powsuffix;
  301.     
  302.       return $ret;
  303.     }
  304.     // }}}
  305. }
  306.  
  307. ?>
  308.