PHP

How to convert digits and numbers to string in PHP

0
1.1k
2018 / 08 / 21

We came across in such requirements where we need to display a number into string format, or need to convert a set of number(s) into string.

We can do so by following snippet:

function convertNumberToString($num){ 
  $decones = array( 
          '01' => "One", 
          '02' => "Two", 
          '03' => "Three", 
          '04' => "Four", 
          '05' => "Five", 
          '06' => "Six", 
          '07' => "Seven", 
          '08' => "Eight", 
          '09' => "Nine", 
          10 => "Ten", 
          11 => "Eleven", 
          12 => "Twelve", 
          13 => "Thirteen", 
          14 => "Fourteen", 
          15 => "Fifteen", 
          16 => "Sixteen", 
          17 => "Seventeen", 
          18 => "Eighteen", 
          19 => "Nineteen" 
  );
  $ones = array( 
          0 => " ",
          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" 
  ); 
  $tens = array( 
          0 => "",
          2 => "Twenty", 
          3 => "Thirty", 
          4 => "Forty", 
          5 => "Fifty", 
          6 => "Sixty", 
          7 => "Seventy", 
          8 => "Eighty", 
          9 => "Ninety" 
  ); 
  $hundreds = array( 
          "Hundred", 
          "Thousand", 
          "Million", 
          "Billion", 
          "Trillion", 
          "Quadrillion" 
  ); //limit t quadrillion 
  $num = number_format($num,2,".",","); 
  $num_arr = explode(".",$num); 
  $wholenum = $num_arr[0]; 
  $decnum = $num_arr[1]; 
  $whole_arr = array_reverse(explode(",",$wholenum)); 
  krsort($whole_arr); 
  $rettxt = ""; 
  foreach($whole_arr as $key => $i){ 
      if($i < 20){ 
          $rettxt .= $ones[$i]; 
      }
      elseif($i < 100){ 
          $rettxt .= $tens[substr($i,0,1)]; 
          $rettxt .= " ".$ones[substr($i,1,1)]; 
      }
      else{ 
          $rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0]; 
          $rettxt .= " ".$tens[substr($i,1,1)]; 
          $rettxt .= " ".$ones[substr($i,2,1)]; 
      } 
      if($key > 0){ 
          $rettxt .= " ".$hundreds[$key]." "; 
      } 

  } 

  if($decnum > 0){ 
      $rettxt .= " point "; 
      if($decnum < 20){ 
          $rettxt .= $decones[$decnum]; 
      }
      elseif($decnum < 100){ 
          $rettxt .= $tens[substr($decnum,0,1)]; 
          $rettxt .= " ".$ones[substr($decnum,1,1)]; 
      }
  } 
  return $rettxt;
}

Usage

The usage of above function is:

echo convertNumberToString('10'); // OUTPUT:  Ten
echo convertNumberToString('123'); // OUTPUT:  One hundred twenty three
echo convertNumberToString('34.67'); // OUTPUT:  Thirty four point sixty seven

 

Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at hamzamehmood.com. Thanks