All for Joomla All for Webmasters

Let's Play With Coding…

Number to Word conversion Using PHP

In some cases we are in position to convert number to word for an instance when we are working on invoice related modules there it required invoice value to be converted into word and print in invoice details for that I found following function which pretty much helpful for me in my projects. Below code will help you in arrive words in millions and billions most of the countries follow that…

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 function number_to_word( $num = '' )
{
    $num    = ( string ) ( ( int ) $num );
   
    if( ( int ) ( $num ) && ctype_digit( $num ) )
    {
        $words  = array( );
       
        $num    = str_replace( array( ',' , ' ' ) , '' , trim( $num ) );
       
        $list1  = array('','one','two','three','four','five','six','seven',
            'eight','nine','ten','eleven','twelve','thirteen','fourteen',
            'fifteen','sixteen','seventeen','eighteen','nineteen');
       
        $list2  = array('','ten','twenty','thirty','forty','fifty','sixty',
            'seventy','eighty','ninety','hundred');
       
        $list3  = array('','thousand','million','billion','trillion',
            'quadrillion','quintillion','sextillion','septillion',
            'octillion','nonillion','decillion','undecillion',
            'duodecillion','tredecillion','quattuordecillion',
            'quindecillion','sexdecillion','septendecillion',
            'octodecillion','novemdecillion','vigintillion');
       
        $num_length = strlen( $num );
        $levels = ( int ) ( ( $num_length + 2 ) / 3 );
        $max_length = $levels * 3;
        $num    = substr( '00'.$num , -$max_length );
        $num_levels = str_split( $num , 3 );
       
        foreach( $num_levels as $num_part )
        {
            $levels--;
            $hundreds   = ( int ) ( $num_part / 100 );
            $hundreds   = ( $hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ( $hundreds == 1 ? '' : 's' ) . ' ' : '' );
            $tens       = ( int ) ( $num_part % 100 );
            $singles    = '';
           
            if( $tens < 20 )
            {
                $tens   = ( $tens ? ' ' . $list1[$tens] . ' ' : '' );
            }
            else
            {
                $tens   = ( int ) ( $tens / 10 );
                $tens   = ' ' . $list2[$tens] . ' ';
                $singles    = ( int ) ( $num_part % 10 );
                $singles    = ' ' . $list1[$singles] . ' ';
            }
            $words[]    = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_part ) ) ? ' ' . $list3[$levels] . ' ' : '' );
        }
       
        $commas = count( $words );
       
        if( $commas > 1 )
        {
            $commas = $commas - 1;
        }
       
        $words  = implode( ', ' , $words );
       
        //Some Finishing Touch
        //Replacing multiples of spaces with one space
        $words  = trim( str_replace( ' ,' , ',' , trim_all( ucwords( $words ) ) ) , ', ' );
        if( $commas )
        {
            $words  = str_replace_last( ',' , ' and' , $words );
        }
       
        return $words;
    }
    else if( ! ( ( int ) $num ) )
    {
        return 'Zero';
    }
    return '';
}
function trim_all( $str , $what = NULL , $with = ' ' )
{
    if( $what === NULL )
    {
        //  Character      Decimal      Use
        //  "\0"            0           Null Character
        //  "\t"            9           Tab
        //  "\n"           10           New line
        //  "\x0B"         11           Vertical Tab
        //  "\r"           13           New Line in Mac
        //  " "            32           Space
       
        $what   = "\\x00-\\x20";    //all white-spaces and control chars
    }
   
    return trim( preg_replace( "/[".$what."]+/" , $with , $str ) , $what );
}
function str_replace_last( $search , $replace , $str ) {
    if( ( $pos = strrpos( $str , $search ) ) !== false ) {
        $search_length  = strlen( $search );
        $str    = substr_replace( $str , $replace , $pos , $search_length );
    }
    return $str;
}

To use above function just call that function

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 echo number_to_word( '25478' );

For South Asian numbering system they use crores so based on that following function will help you with implementing it.

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 function singledigit($number){
        switch($number){
            case 0:$word = "zero";break;
            case 1:$word = "One";break;
            case 2:$word = "two";break;
            case 3:$word = "three";break;
            case 4:$word = "Four";break;
            case 5:$word = "Five";break;
            case 6:$word = "Six";break;
            case 7:$word = "Seven";break;
            case 8:$word = "Eight";break;
            case 9:$word = "Nine";break;
        }
        return $word;
    }

    function doubledigitnumber($number){
        if($number == 0){
            $word = "";
        }
        else{
            $word = singledigit($number);
        }       
        return $word;
    }

    function doubledigit($number){
        switch($number[0]){
            case 0:$word = doubledigitnumber($number[1]);break;
            case 1:
                switch($number[1]){
                    case 0:$word = "Ten";break;
                    case 1:$word = "Eleven";break;
                    case 2:$word = "Twelve";break;
                    case 3:$word = "Thirteen";break;
                    case 4:$word = "Fourteen";break;
                    case 5:$word = "Fifteen";break;
                    case 6:$word = "Sixteen";break;
                    case 7:$word = "Seventeen";break;
                    case 8:$word = "Eighteen";break;
                    case 9:$word = "Ninteen";break;
                }break;
            case 2:$word = "Twenty".doubledigitnumber($number[1]);break;                
            case 3:$word = "Thirty".doubledigitnumber($number[1]);break;
            case 4:$word = "Forty".doubledigitnumber($number[1]);break;
            case 5:$word = "Fifty".doubledigitnumber($number[1]);break;
            case 6:$word = "Sixty".doubledigitnumber($number[1]);break;
            case 7:$word = "Seventy".doubledigitnumber($number[1]);break;
            case 8:$word = "Eighty".doubledigitnumber($number[1]);break;
            case 9:$word = "Ninety".doubledigitnumber($number[1]);break;

        }
        return $word;
    }

    function unitdigit($numberlen,$number){
        switch($numberlen){         
            case 3:$word = "Hundred";break;
            case 4:$word = "Thousand";break;
            case 5:$word = "Thousand";break;
            case 6:$word = "Lakh";break;
            case 7:$word = "Lakh";break;
            case 8:$word = "Crore";break;
            case 9:$word = "Crore";break;

        }
        return $word;
    }

    function numberToWord($number){

        $numberlength = strlen($number);
        if ($numberlength == 1) { 
            return singledigit($number);
        }elseif ($numberlength == 2) {
            return doubledigit($number);
        }
        else {

            $word = "";
            $wordin = "";

            if($numberlength == 9){
                if($number[0] >0){
                    $unitdigit = unitdigit($numberlength,$number[0]);
                    $word = doubledigit($number[0].$number[1]) ." ".$unitdigit." ";
                    return $word." ".numberToWord(substr($number,2));
                }
                else{
                    return $word." ".numberToWord(substr($number,1));
                }
            }

            if($numberlength == 7){
                if($number[0] >0){
                    $unitdigit = unitdigit($numberlength,$number[0]);
                    $word = doubledigit($number[0].$number[1]) ." ".$unitdigit." ";
                    return $word." ".numberToWord(substr($number,2));
                }
                else{
                    return $word." ".numberToWord(substr($number,1));
                }

            }

            if($numberlength == 5){
                if($number[0] >0){
                    $unitdigit = unitdigit($numberlength,$number[0]);
                    $word = doubledigit($number[0].$number[1]) ." ".$unitdigit." ";
                    return $word." ".numberToWord(substr($number,2));
                }
                else{
                    return $word." ".numberToWord(substr($number,1));
                }


            }
            else{
                if($number[0] >0){
                    $unitdigit = unitdigit($numberlength,$number[0]);
                    $word = singledigit($number[0]) ." ".$unitdigit." ";
                }               
                return $word." ".numberToWord(substr($number,1));
            }
        }
    }

To use above function just call that function by passing value to it

/**
 * CodeZone.in
 * Author: Dineshkumar
 * Contact : hi@codezone.in
 */
 $number=5647895;
 echo numberToWord($number);

 

Related posts

ASCII to HEX Convertion in PHP

In some cases we are in position to convert number to word for an instance when we are working on invoice related modules there it required invoice value to be converted into word and

Delete an Element from Array

In some cases we are in position to convert number to word for an instance when we are working on invoice related modules there it required invoice value to be converted into word and

Leave a Comment

Leave a Reply