Convert Number To Words Using PHP

Kailash Singh

Convert Number To Words Using PHP

Published Mar 02,2022 by Kailash Singh

0 Comment     232 Views    


In this tutorial, we are going to teach you. How to convert number to word in php.

 

Complete code of source code:

<?php
$convertNumber = $sendNumber = '';
  if(isset($_POST['submit']))
  {
      $sendNumber = $_POST['number'];
      
      if($_POST['number'] != '' && $_POST['number'] > 0)
      {
         $number = $_POST['number'];
           $no = floor($number);
           $point = round($number - $no, 2) * 100;
           $hundred = null;
           $digits_1 = 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_1) {
             $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;
          }
          $str = array_reverse($str);
          $result = implode('', $str);
          $points = ($point) ?
            ". " . $words[$point / 10] . " " . 
                  $words[$point = $point % 10] : '';
         $convertNumber = ucwords(strtolower($result . $points)) . " Rupees Only";

       }
  }
      
  
 ?>


<?php 
  if($sendNumber > 0){ 
?>
  <h4 align="center">
      <?php echo $sendNumber; ?> : <span class="text-success"><?php echo $convertNumber; ?></span>
  </h4>
<?php } ?>

<form action="" method="POST">

    <input type="text" name="number" class="form-control" placeholder="Enter Number" required><br>

    <button name="submit" type="submit" class="btn btn-primary">Convert</button>

</form>

 


Comments ( 0 )