Pouknouki

Convert numbers to text (22 -> twenty-two) (FR) (PHP)

May 27th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.72 KB | None | 0 0
  1. function numberToText($number) {
  2.  
  3.     $hyphen      = '-';
  4.     $conjunction = ' ';
  5.     $separator   = ' ';
  6.     $negative    = 'moins ';
  7.     $decimal     = ', ';
  8.     $dictionary  = array(
  9.         0                   => 'zéro',
  10.         1                   => 'un',
  11.         2                   => 'deux',
  12.         3                   => 'trois',
  13.         4                   => 'quatre',
  14.         5                   => 'cinq',
  15.         6                   => 'six',
  16.         7                   => 'sept',
  17.         8                   => 'huit',
  18.         9                   => 'neuf',
  19.         10                  => 'dix',
  20.         11                  => 'onze',
  21.         12                  => 'douze',
  22.         13                  => 'treize',
  23.         14                  => 'quatorze',
  24.         15                  => 'quinze',
  25.         16                  => 'seize',
  26.         17                  => 'dix-sept',
  27.         18                  => 'dix-huit',
  28.         19                  => 'dix-neuf',
  29.         20                  => 'vingt',
  30.         30                  => 'trente',
  31.         40                  => 'quarante',
  32.         50                  => 'cinquante',
  33.         60                  => 'soixante',
  34.         70                  => 'soixante-dix',
  35.         80                  => 'quatre-vingt',
  36.         90                  => 'quatre-vingt-dix',
  37.         100                 => 'cent',
  38.         1000                => 'mille',
  39.         1000000             => 'million',
  40.         1000000000          => 'milliard',
  41.         1000000000000       => 'trillion',
  42.         1000000000000000    => 'quadrillion',
  43.         1000000000000000000 => 'quintillion'
  44.     );
  45.  
  46.     if (!is_numeric($number)) {
  47.         return false;
  48.     }
  49.  
  50.     if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
  51.         // overflow
  52.         trigger_error(
  53.             'numberToText n'accepte que les nombres compris entre&nbsp;-' . PHP_INT_MAX . ' et&nbsp;' . PHP_INT_MAX,
  54.            E_USER_WARNING
  55.        );
  56.        return false;
  57.    }
  58.  
  59.    if ($number < 0) {
  60.        return $negative . numberToText(abs($number));
  61.    }
  62.  
  63.    $string = $fraction = null;
  64.  
  65.    if (strpos($number, '.') !== false) {
  66.        list($number, $fraction) = explode('.', $number);
  67.    }
  68.  
  69.    switch (true) {
  70.        case $number < 21:
  71.            $string = $dictionary[$number];
  72.            break;
  73.        case $number < 100:
  74.            $tens   = ((int) ($number / 10)) * 10;
  75.            $units  = $number % 10;
  76.            $string = $dictionary[$tens];
  77.            if ($units) {
  78.                $string .= $hyphen . $dictionary[$units];
  79.            }
  80.            break;
  81.        case $number < 1000:
  82.            $hundreds  = $number / 100;
  83.            $remainder = $number % 100;
  84.            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
  85.            if ($remainder) {
  86.                $string .= $conjunction . numberToText($remainder);
  87.            }
  88.            break;
  89.        default:
  90.            $baseUnit = pow(1000, floor(log($number, 1000)));
  91.            $numBaseUnits = (int) ($number / $baseUnit);
  92.            $remainder = $number % $baseUnit;
  93.            $string = numberToText($numBaseUnits) . ' ' . $dictionary[$baseUnit];
  94.            if ($remainder) {
  95.                $string .= $remainder < 100 ? $conjunction : $separator;
  96.                $string .= numberToText($remainder);
  97.            }
  98.            break;
  99.    }
  100.  
  101.    if (null !== $fraction && is_numeric($fraction)) {
  102.        $string .= $decimal;
  103.        $words = array();
  104.        foreach (str_split((string) $fraction) as $number) {
  105.            $words[] = $dictionary[$number];
  106.        }
  107.        $string .= implode(' ', $words);
  108.    }
  109.  
  110.    return $string;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment