Thursday, January 19, 2012

Converting Number to Letter

Converting Number to Letter:
Example,Converting 28 to Twenty Eight in php..

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] = '';
   }

   return trim(implode(' ', $result));
}

No comments:

Post a Comment