views:

321

answers:

7

Does anyone know how to convert a number such as 1, 2, or 3 to their text version (one, two, three) in PHP? I only need to convert from 1 to 99. I know I could write a huge switch statement but that would be ridiculous.

A: 
$numbers_as_words = array("zero", "one", "two"..."ninety nine");
echo $numbers_as_words[1] // prints one
echo $numbers_as_words[2] // prints two
echo $numbers_as_words[54] // prints fifty four

etc...

John Conde
+2  A: 

Not really ideal, but atleast better than a 'huge switch statement':

 $numbermappings = array("zero", "one","two","three", "four" .... "ninetynine");
 echo  $numbermappings[4]; // four

You still have to write that huge array though..

Per Holmäng
+22  A: 

pear has a package Numbers_Words:

$numberToWord = new Numbers_Words();
echo $numberToWords->toWords(200);
flurin
+1 Didn't know that existed.
John Conde
+1 Looks spot on to me.
middaparka
Or just: `Numbers_Words::toWords(200)`
Felix Kling
A: 

Here are two functions that do this:

http://www.php.net/manual/en/function.number-format.php#66895

and

http://fundisom.com/phparadise/php/string_handling/number_to_words

There is even a PEAR package to do this.

Felix Kling
How can number_format do this? That comment you linked to is just an array lookup which has little to nothing to do with number_format.
middaparka
WOW... The code on that last link is crazy! I'm afraid it's gonna eat me!!
Alix Axel
@middaparka: It is not my fault that the commentator commented on this site...
Felix Kling
Well it is what I found using Google... don't blame me ;)
Felix Kling
+2  A: 

See this function in action:

function N2L($number)
{
    $result = array();
    $tens = floor($number / 10);
    $units = $number % 10;

    $words = array
    (
        'units' => array('', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eightteen', 'Nineteen'),
        'tens' => array('', '', 'Twenty', 'Thirty', 'Fourty', 'Fifty', 'Sixty', 'Seventy', 'Eigthy', 'Ninety')
    );

    if ($tens < 2)
    {
        $result[] = $words['units'][$tens * 10 + $units];
    }

    else
    {
        $result[] = $words['tens'][$tens];

        if ($units > 0)
        {
            $result[count($result) - 1] .= '-' . $words['units'][$units];
        }
    }

    if (empty($result[0]))
    {
        $result[0] = 'Zero';
    }

    return trim(implode(' ', $result));
}
Alix Axel
A: 

There's a PEAR package that does this. It does number WAY higher than 99 and is multi-language, so it might be more heavyweight than you need, but still worth checking out:

http://pear.php.net/package/Numbers_Words

danieltalsky
+3  A: 

Here's one I wrote way back in college. It includes support for negative numbers, as well. I know there's some ways it could be shortened and/or cleaned up, but hey, it works well for any integer!

/**
 Converts an integer to its textual representation.
 @param num the number to convert to a textual representation
 @param depth the number of times this has been recursed
*/
function readNumber($num, $depth=0)
{
    $num = (int)$num;
    $retval ="";
    if ($num < 0) // if it's any other negative, just flip it and call again
        return "negative " + readNumber(-$num, 0);
    if ($num > 99) // 100 and above
    {
        if ($num > 999) // 1000 and higher
            $retval .= readNumber($num/1000, $depth+3);

        $num %= 1000; // now we just need the last three digits
        if ($num > 99) // as long as the first digit is not zero
            $retval .= readNumber($num/100, 2)." hundred\n";
        $retval .=readNumber($num%100, 1); // our last two digits                       
    }
    else // from 0 to 99
    {
        $mod = floor($num / 10);
        if ($mod == 0) // ones place
        {
            if ($num == 1) $retval.="one";
            else if ($num == 2) $retval.="two";
            else if ($num == 3) $retval.="three";
            else if ($num == 4) $retval.="four";
            else if ($num == 5) $retval.="five";
            else if ($num == 6) $retval.="six";
            else if ($num == 7) $retval.="seven";
            else if ($num == 8) $retval.="eight";
            else if ($num == 9) $retval.="nine";
        }
        else if ($mod == 1) // if there's a one in the ten's place
        {
            if ($num == 10) $retval.="ten";
            else if ($num == 11) $retval.="eleven";
            else if ($num == 12) $retval.="twelve";
            else if ($num == 13) $retval.="thirteen";
            else if ($num == 14) $retval.="fourteen";
            else if ($num == 15) $retval.="fifteen";
            else if ($num == 16) $retval.="sixteen";
            else if ($num == 17) $retval.="seventeen";
            else if ($num == 18) $retval.="eighteen";
            else if ($num == 19) $retval.="nineteen";
        }
        else // if there's a different number in the ten's place
        {
            if ($mod == 2) $retval.="twenty ";
            else if ($mod == 3) $retval.="thirty ";
            else if ($mod == 4) $retval.="forty ";
            else if ($mod == 5) $retval.="fifty ";
            else if ($mod == 6) $retval.="sixty ";
            else if ($mod == 7) $retval.="seventy ";
            else if ($mod == 8) $retval.="eighty ";
            else if ($mod == 9) $retval.="ninety ";
            if (($num % 10) != 0)
            {
                $retval = rtrim($retval); //get rid of space at end
                $retval .= "-";
            }
            $retval.=readNumber($num % 10, 0);
        }
    }

    if ($num != 0)
    {
        if ($depth == 3)
            $retval.=" thousand\n";
        else if ($depth == 6)
            $retval.=" million\n";
        if ($depth == 9)
            $retval.=" billion\n";
    }
    return $retval;
}
pib