In the world of PHP programming, there are times when you want numbers to speak a language everyone understands words. This guide will take you through an easy process of how to convert digits and numbers to words in PHP. Let’s explore step-by-step how to add a touch of clarity and simplicity to your numeric displays.
Table of Contents
Introduction
We came across such requirements where we need to display a number in words or convert a set of number(s) into a string. This article will show 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. The function will convert digits and numbers to words in PHP. This will compare the combinations and generate words for each number.
The implementation in PHP is as follows:
<?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; } ?>
The above snippet will convert digits and numbers to words in PHP. In this script, we have used several PHP built-in functions that are explained 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.
Testing and Tweaking: Making Sure It Works
Before using this in your actual PHP projects, it’s important to test your number-to-words function thoroughly. Make sure it works accurately and adjust the logic if needed for different situations.
Conclusion: Friendly Numbers in PHP
You’ve successfully made your PHP applications friendlier by adding a tool: convert digits and numbers to words in PHP. This simple tweak makes your numeric values more readable and contributes to a user-friendly interface.
By incorporating number-to-words conversion in your PHP toolkit, you’re not just coding; you’re creating applications that speak a language everyone can understand.