Javascript Disabled!

Please Enable Javascript if you disabled it, or use another browser we preferred Google Chrome. Please Refresh Page After EnablePowered By UnCopy Plugin.

How to convert numbers into words in PHP

  • PHP code defines a function called fnConvertNumberToWords that takes a numerical input and converts it into its word representation. The function breaks down the number into parts (e.g., thousands, hundreds) and looks up the corresponding word representation for each part. It uses arrays to store the words for single-digit numbers, two-digit numbers, and suffixes like “Hundred,” “Thousand,” etc. The final word representation is returned as a single string. However, this code may have limitations and may not handle extremely large numbers effectively.
				
					function fnConvertNumberToWords($num)
{
    $number = $num;
    $no = floor($number);
    $hundred = null;
    $digits = strle($n); //to find the length of the number
    $i = 0;
    // Numbers can be stored in array format
    $str = array();
    $words = 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', '20' => 'Twenty',
    '30' => 'Thirty', '40' => 'Forty', '50' => 'Fifty',
    '60' => 'Sixty', '70' => 'Seventy',
    '80' => 'Eighty', '0' => 'Ninety');
    $digits = array('', 'Hundred', 'Thousand', 'lakh', 'Crore');

				
			
  • The function fnConvertNumberToWords takes a numerical input $num as a parameter.
  • $number is assigned the value of $num.
  • $no is assigned the floor value of $number, removing any decimal part.
  • $hundred is initialized as null.
  • $digits_1 is assigned the length of $no, representing the number of digits in the input number.
  • $i is initialized to 0, which will be used as an index for the while loop.
  • $str is initialized as an empty array, which will be used to store the words for each part of the number.
  • $words is an associative array that maps numbers from 0 to 90 to their respective word representation. For example, ‘1’ maps to ‘One’, ‘2’ maps to ‘Two’, ’10’ maps to ‘Ten’, etc.
  • $digits is an array that stores the suffixes for various positions in the number, such as ‘Hundred’, ‘Thousand’, ‘lakh’, and ‘Crore’.
  •  
				
					    // Extract the last digit of the number and print the corresponding number in words until the number becomes 0
    while ($i < $digits_1)
    {
        $divider = ($i == 2) ? 10 : 100;
        // Round numbers down to the nearest integer
        $number = floor($no % $divider);
        $no = floor($no / $divider);
        $i += ($divider == 10) ? 1 : 0;

        if ($number)
        {
            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;

				
			
  • The code enters a while loop that runs as long as $i is less than $digits_1, meaning it processes each part of the number from right to left.
  • The $divider variable is set to either 10 or 100 depending on the position of the digit. If it’s at the hundredth place (i.e., 2nd digit from the right), the divider is set to 10; otherwise, it’s set to 100.
  • The last digit of the number is extracted using the modulo operation ($no % $divider), and the remaining part of the number is updated by dividing the number by the divider and taking the floor ($no = floor($no / $divider)). This process separates the number into parts, making it easier to convert each part into words.
  • The variable $counter is assigned the count of elements in the $str array, which will be used to determine the position of the current part of the number.
  • If the $number is not zero, the code proceeds to convert it into words.
  •  
				
					            $str[] = ($number < 21) ? $words[$number] . " " .
            $digits[$counter] .
            $plural . " " .
            $hundred : $words[floor($number / 10) * 10]. " " .
            $words[$number % 10] . " ".
            $digits[$counter] . $plural . " " .
            $hundred;
        } else {
            $str[] = null;
        }
    }

				
			
  • The code checks whether the $number is less than 21. If it is, it directly looks up the word representation from the $words array and concatenates it with the corresponding suffix ($digits[$counter]).
  • If the $number is greater than 20, the code finds the word representation of the tens place ($words[floor($number / 10) * 10]) and the word representation of the ones place ($words[$number % 10]) separately. These two words are then concatenated with the corresponding suffix ($digits[$counter]) and $plural if needed.
  • The converted word representation of the current part of the number is added to the $str array.
  •  
				
					    $str = array_reverse($str);
    $result = implode('', $str); 
    return $result ;
}

				
			
  • After the while loop, the $str array is reversed, as the conversion started from the rightmost part of the number and proceeded towards the leftmost part.
  • The array elements are then concatenated together using the implode function, resulting in a single string representing the word representation of the entire number.
  • The final result is returned.

This code takes a numerical input and converts it into its word representation. For example, if you call the function with the input 12345, it will return "Twelve Thousand Three Hundred Forty-Five". Please note that the code may have some limitations and may not work correctly for very large numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock