Jump to content

Converting Numbers to Textual Descriptions


Centinul

Recommended Posts

Couldn't find anything, so I wrote a quick example of how you could do it. I didn't to any sort of optimization/etc. I used arrays to remove the need for a separate function w/ switches and/or if's. Just an idea.

 

<?php
(c) no_one 

$int1Str = array("zero","one","two","three","four","five","six","seven","eight","nine");
$int10Str = array(1=>"ten",2=>"twenty",3=>"thirty",4=>"forty",5=>"fifty",6=>"sixty",7=>"seventy",8=>"eighty",9=>"ninety");

function intString($num)
{
    global $int1Str, $int10Str;
    $hold = $num;
    $istr = "";
    
    while( $hold !== true ) {
        if( $hold >= 100 ) 
        {
            if ( $hold >= 1000 ) 
            {
                $hmult = 1000;
                $hms = " thousand ";
            }
            else { // hundred
                $hmult = 100;
                $hms = " hundred ";
            }
            $hm = (int)($hold/$hmult);
            $hold -= $hm*$hmult;
            
            $istr .= $int1Str[$hm] . $hms;
        }
        else if ( $hold >= 10 )
        {
            $hm = (int)($hold/10);
            $hold -= $hm*10; // could probably do something better here
            
            $istr .= $int10Str[$hm] . " ";
        }
        else {
            if ( $hold || (!$hold && empty($istr)) )
            { // sloppy?
                $istr .= $int1Str[$hold];
            }
            
            $hold = true; // kill loop
        }
    }
    
    return $istr;
}

print("<br/>" . intString(8137));



?>

 

eight thousand one hundred thirty seven

not much needed a change, ten through nineteen should come through now. Ty for the heads up, kind of hacked it out quick.

 

<?php
$int1Str = array("zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen");
$int10Str = array(1=>"ten",2=>"twenty",3=>"thirty",4=>"forty",5=>"fifty",6=>"sixty",7=>"seventy",8=>"eighty",9=>"ninety");

function intString($num)
{
    global $int1Str, $int10Str;
    $hold = $num;
    $istr = "";
    
    while( $hold !== true ) {
        if( $hold >= 100 ) 
        {
            if ( $hold >= 1000 ) 
            {
                $hmult = 1000;
                $hms = " thousand ";
            }
            else { // hundred
                $hmult = 100;
                $hms = " hundred ";
            }
            $hm = (int)($hold/$hmult);
            $hold -= $hm*$hmult;
            
            $istr .= $int1Str[$hm] . $hms;
        }
        else if ( $hold >= 20 )
        {
            $hm = (int)($hold/10);
            $hold -= $hm*10; // could probably do something better here
            
            $istr .= $int10Str[$hm] . " ";
        }
        else {
            if ( $hold || (!$hold && empty($istr)) )
            {
                $istr .= $int1Str[$hold];
            }
            
            $hold = true; // kill loop
        }
    }
    
    return $istr;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.