How to convert digits and numbers to words in PHP

How to convert digits and numbers to strings in PHP

We came across such requirements where we need to display a number in words, or need to convert a set of number(s) into a string. In this article, we will see how to convert digits and numbers to words in PHP.

Let’s write some code to convert digits and numbers to words in PHP

Let’s write a function that will take a number as a function parameter. The function will process the input, parse it and divide it into combinations of units/tens/hundreds, etc. This will compare the combinations and generate words for each number.

The implementation in PHP is as below:

<?php
// function to Convert digits and numbers to words in PHP.
	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 till quadrillion 

	  // format a number upto 2 decimals
		$num = number_format($num,2,".",","); 
		$num_arr = explode(".",$num); 
		$wholenum = $num_arr[0]; 
		$decnum = $num_arr[1]; 
		$whole_arr = array_reverse(explode(",",$wholenum));
		// sorts an associative array in descending order 
		krsort($whole_arr); 
		$rettxt = ""; 

		// iterate through the array
		foreach($whole_arr as $key => $i){ 
			if($i < 20){ 
				$rettxt .= $ones[$i]; 
			}
			elseif($i < 100){ 
				// the substr function returns part of string
				$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 float is found, show point
	  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;
	}
?>

In the above snippet, we have used a number of PHP built-in functions which we will explain below:

  • number_format()formats a number with grouped thousands and optionally decimal digits.
  • explode() breaks a string into an array.
  • array_reverse()returns a new array with the order of the elements reversed.
  • krsort()sorts an associative array in descending order, according to the key.
  • substr()returns a part of a string.

Usage:

The usage of the above function is:

<?php
	echo convertNumberToString('10'); 
	// OUTPUT:  Ten

	echo convertNumberToString('123'); 
	// OUTPUT:  One hundred twenty three
	
	echo convertNumberToString('3467'); 
	// OUTPUT:  Three thousand four hundred sixty seven
?>

The function also supports float numbers:

<?php
	echo convertNumberToString('1.23'); 
	// OUTPUT:  One point twenty three
	
	echo convertNumberToString('34.67'); 
	// OUTPUT:  Thirty four point sixty seven
?>

Limitation

This function has a limit of numbers to Quadrillion. You can customize the function as per your needs.

Related Posts