Skip to content

Instantly share code, notes, and snippets.

@mkdesignn
Created October 26, 2017 10:04
Show Gist options
  • Save mkdesignn/a44895a2879d1c8073a42e36f5fc95aa to your computer and use it in GitHub Desktop.
Save mkdesignn/a44895a2879d1c8073a42e36f5fc95aa to your computer and use it in GitHub Desktop.
TranslateToWords
function translateToWords($number)
{
/*****
* A recursive function to turn digits into words
* Numbers must be integers from -999,999,999,999 to 999,999,999,999 inclussive.
*
* (C) 2010 Peter Ajtai
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See the GNU General Public License: <http://www.gnu.org/licenses/>.
*
*/
// zero is a special case, it cause problems even with typecasting if we don't deal with it here
$max_size = pow(10,18);
if (!$number) return "zero";
if (is_int($number) && $number < abs($max_size))
{
switch ($number)
{
// set up some rules for converting digits to words
case $number < 0:
$prefix = "negative";
$suffix = translateToWords(-1*$number);
$string = $prefix . " " . $suffix;
break;
case 1:
$string = "یک";
break;
case 2:
$string = "دو";
break;
case 3:
$string = "سی";
break;
case 4:
$string = "چهار";
break;
case 5:
$string = "پنج";
break;
case 6:
$string = "شش";
break;
case 7:
$string = "هفت";
break;
case 8:
$string = "هشت";
break;
case 9:
$string = "9";
break;
case 10:
$string = "ده";
break;
case 11:
$string = "یازده";
break;
case 12:
$string = "دوازده";
break;
case 13:
$string = "سیزده";
break;
case 14:
$string = "چهارده";
break;
case 15:
$string = "پانزده";
break;
case 16:
$string = "شانزده";
break;
case 17:
$string = "هفده";
break;
case 18:
$string = "هجده";
break;
case 19:
$string = "نونزده";
break;
case 20:
$string = "بیست";
break;
case 30:
$string = "سی";
break;
case 40:
$string = "چهل";
break;
case 50:
$string = "پنجاه";
break;
case 60:
$string = "شصت";
break;
case 70:
$string = "هغتاد";
break;
case 80:
$string = "هشتاد";
break;
case 90:
$string = "نود";
break;
case $number < 100:
$prefix = translateToWords($number-$number%10);
$suffix = translateToWords($number%10);
$string = $prefix . " و " . $suffix;
break;
// handles all number 100 to 999
case $number < pow(10,3):
// floor return a float not an integer
$prefix = translateToWords(intval(floor($number/pow(10,2)))) . "صد";
if ($number%pow(10,2)) $suffix = " و " . translateToWords($number%pow(10,2));
$string = $prefix . $suffix;
break;
case $number < pow(10,6):
// floor return a float not an integer
$prefix = translateToWords(intval(floor($number/pow(10,3)))) . " هزار";
if ($number%pow(10,3)) $suffix = translateToWords($number%pow(10,3));
$string = $prefix . " " . $suffix;
break;
case $number < pow(10,9):
// floor return a float not an integer
$prefix = translateToWords(intval(floor($number/pow(10,6)))) . " میلیون";
if ($number%pow(10,6)) $suffix = translateToWords($number%pow(10,6));
$string = $prefix . " " . $suffix;
break;
case $number < pow(10,12):
// floor return a float not an integer
$prefix = translateToWords(intval(floor($number/pow(10,9)))) . " میلیارد";
if ($number%pow(10,9)) $suffix = translateToWords($number%pow(10,9));
$string = $prefix . " " . $suffix;
break;
case $number < pow(10,15):
// floor return a float not an integer
$prefix = translateToWords(intval(floor($number/pow(10,12)))) . " تیلیارد";
if ($number%pow(10,12)) $suffix = translateToWords($number%pow(10,12));
$string = $prefix . " " . $suffix;
break;
// Be careful not to pass default formatted numbers in the quadrillions+ into this function
// Default formatting is float and causes errors
case $number < pow(10,18):
// floor return a float not an integer
$prefix = translateToWords(intval(floor($number/pow(10,15)))) . " quadrillion";
if ($number%pow(10,15)) $suffix = translateToWords($number%pow(10,15));
$string = $prefix . " " . $suffix;
break;
}
} else
{
echo "ERROR with - $number<br/> Number must be an integer between -" . number_format($max_size, 0, ".", ",") . " and " . number_format($max_size, 0, ".", ",") . " exclussive.";
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment