Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function numberToText($number) {
- $hyphen = '-';
- $conjunction = ' ';
- $separator = ' ';
- $negative = 'moins ';
- $decimal = ', ';
- $dictionary = array(
- 0 => 'zéro',
- 1 => 'un',
- 2 => 'deux',
- 3 => 'trois',
- 4 => 'quatre',
- 5 => 'cinq',
- 6 => 'six',
- 7 => 'sept',
- 8 => 'huit',
- 9 => 'neuf',
- 10 => 'dix',
- 11 => 'onze',
- 12 => 'douze',
- 13 => 'treize',
- 14 => 'quatorze',
- 15 => 'quinze',
- 16 => 'seize',
- 17 => 'dix-sept',
- 18 => 'dix-huit',
- 19 => 'dix-neuf',
- 20 => 'vingt',
- 30 => 'trente',
- 40 => 'quarante',
- 50 => 'cinquante',
- 60 => 'soixante',
- 70 => 'soixante-dix',
- 80 => 'quatre-vingt',
- 90 => 'quatre-vingt-dix',
- 100 => 'cent',
- 1000 => 'mille',
- 1000000 => 'million',
- 1000000000 => 'milliard',
- 1000000000000 => 'trillion',
- 1000000000000000 => 'quadrillion',
- 1000000000000000000 => 'quintillion'
- );
- if (!is_numeric($number)) {
- return false;
- }
- if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
- // overflow
- trigger_error(
- 'numberToText n'accepte que les nombres compris entre -' . PHP_INT_MAX . ' et ' . PHP_INT_MAX,
- E_USER_WARNING
- );
- return false;
- }
- if ($number < 0) {
- return $negative . numberToText(abs($number));
- }
- $string = $fraction = null;
- if (strpos($number, '.') !== false) {
- list($number, $fraction) = explode('.', $number);
- }
- switch (true) {
- case $number < 21:
- $string = $dictionary[$number];
- break;
- case $number < 100:
- $tens = ((int) ($number / 10)) * 10;
- $units = $number % 10;
- $string = $dictionary[$tens];
- if ($units) {
- $string .= $hyphen . $dictionary[$units];
- }
- break;
- case $number < 1000:
- $hundreds = $number / 100;
- $remainder = $number % 100;
- $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
- if ($remainder) {
- $string .= $conjunction . numberToText($remainder);
- }
- break;
- default:
- $baseUnit = pow(1000, floor(log($number, 1000)));
- $numBaseUnits = (int) ($number / $baseUnit);
- $remainder = $number % $baseUnit;
- $string = numberToText($numBaseUnits) . ' ' . $dictionary[$baseUnit];
- if ($remainder) {
- $string .= $remainder < 100 ? $conjunction : $separator;
- $string .= numberToText($remainder);
- }
- break;
- }
- if (null !== $fraction && is_numeric($fraction)) {
- $string .= $decimal;
- $words = array();
- foreach (str_split((string) $fraction) as $number) {
- $words[] = $dictionary[$number];
- }
- $string .= implode(' ', $words);
- }
- return $string;
- }
Advertisement
Add Comment
Please, Sign In to add comment