Skip to content

Instantly share code, notes, and snippets.

@bainternet
Created June 11, 2013 10:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save bainternet/5756049 to your computer and use it in GitHub Desktop.
Save bainternet/5756049 to your computer and use it in GitHub Desktop.
a Simple class to convert number to words in php based on http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/
<?php
//simple class to convert number to words in php based on http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/
if ( !class_exists('NumbersToWords') ){
/**
* NumbersToWords
*/
class NumbersToWords{
public static $hyphen = '-';
public static $conjunction = ' and ';
public static $separator = ', ';
public static $negative = 'negative ';
public static $decimal = ' point ';
public static $dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
public static function convert($number){
if (!is_numeric($number) ) return false;
$string = '';
switch (true) {
case $number < 21:
$string = self::$dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = self::$dictionary[$tens];
if ($units) {
$string .= self::$hyphen . self::$dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = self::$dictionary[$hundreds] . ' ' . self::$dictionary[100];
if ($remainder) {
$string .= self::$conjunction . self::convert($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = self::convert($numBaseUnits) . ' ' . self::$dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? self::$conjunction : self::$separator;
$string .= self::convert($remainder);
}
break;
}
return $string;
}
}//end class
}//end if
/**
* usage:
*/
//echo NumbersToWords::convert(2839);
@anildsvv14
Copy link

anildsvv14 commented Mar 8, 2017

wrong function when i enter
$nw=new NumbersToWords();
echo $nw->convert(566560)
it shows me Five Hundreds Sixty Six Thousand and Five Hundreds Sixty that is wrong

@aurellemeless
Copy link

Hi, Now its possible to do that natively with php NumberFormatter class ( PHP 5 >= 5.3.0, PHP 7)

$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format(566560); // outpout : five hundred sixty-six thousand five hundred sixty

@mahbub-shohag
Copy link

It is a wrong function.Time wasting.

@fahaadsam
Copy link

chuss

@fahaadsam
Copy link

chuss

@sumantabag19
Copy link

not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment