How to Convert Number to Words using PHP

In this Tutorial, I will show How to convert number to words using PHP. We will explain convert number to words using PHP. we have to need the Convert Currency Number to Word Format Using PHP. so I can easily Convert Number to Word using here following tutorials.

This feature is very important and you can use the feature for displaying the invoice amount in the custom application software, billing software.

Convert Number to Words in Indian currency

You can convert number to words as a Indian currency. we have a create a function to create any number to word. There are some logic to make the Convert any Number to words in Indian currency.

index.php

<?php

	function getConvertNumberToWord(float $number)
	{
	    $decimal = round($number - ($no = floor($number)), 2) * 100;
	    $hundred = null;
	    $digits_length = strlen($no);
	    $i = 0;
	    $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', 
	        90 => 'Ninety'
	    );

	    $digits = array('', 'Hundred','Thousand','Lakh', 'Crore');

	    while( $i < $digits_length ) {
	        $divider = ($i == 2) ? 10 : 100;
	        $number = floor($no % $divider);
	        $no = floor($no / $divider);
	        $i += $divider == 10 ? 1 : 2;
	        if ($number) {
	            $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
	            $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
	            $str [] = ($number < 21) ? $words[$number].' '. $digits[$counter]. $plural.' '.$hundred:$words[floor($number / 10) * 10].' '.$words[$number % 10]. ' '.$digits[$counter].$plural.' '.$hundred;
	        } else {
	        	$str[] = null;
	        }
	    }

	    $Rupees = implode('', array_reverse($str));
	    $paise = ($decimal > 0) ? "." . ($words[$decimal / 10] . " " . $words[$decimal % 10]) . ' Paise' : '';

	    return ($Rupees ? $Rupees . 'Rupees ' : '') . $paise;
	}
?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Convert Number to Words in Indian currency format with paise value</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  <body>
    <div class="container">
    	<div class="row">
   			<h1 class="text-center mt-5 mb-5">Convert Number to Words in Indian Currency</h1>
    		<div class="col-md-6 offset-3">
    			<form action="" method="POST">
				  	<div class="mb-3">
				    	<label for="amount" class="form-label">Amount</label>
				    	<input type="number" class="form-control" name="amount" placeholder="Enter Amount">
				 	</div>
				  <button type="submit" name="submit" class="btn btn-primary">Submit</button>
				</form>
    		</div>
    		<?php 
				if (isset($_POST['submit'])) {
					$amount = $_POST['amount'];
					echo "<h2 class='text-center mt-3 text-success'>".getConvertNumberToWord($amount)."</h2>";
				}
			?>
    	</div>
    </div>
  </body>
</html>

Output

How to Convert Number to Words using PHP
How to Convert Number to Words using PHP

Conclusion

I hope this tutorial will help you to convert numbers to words in PHP. If there is any doubt then please leave a comment below.

FAQ (Frequently Asked Questions)

How to convert number to text in PHP?

Method 1: Using strval() function. Note: The strval() function is an inbuilt function in PHP and is used to convert any scalar value (string, integer, or double) to a string. We cannot use strval() on arrays or on object, if applied then this function only returns the type name of the value being converted.

Leave a Reply

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