Advertisement
Virajsinh

Number to Word Convert Indian Currency in Php

Sep 11th, 2024 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | Source Code | 0 0
  1. <php
  2. // Ref : https://stackoverflow.com/questions/45276116/number-to-words-conversion-indian-rupee-decimal-string-issue-in-php
  3. // Indian Digit Rupees To Word  
  4.  
  5. // Support Point After Two Digit Only
  6. function convertNumberToIndianCurrency($number)
  7. {
  8.     $no = (int)floor($number);
  9.     $point = (int)round(($number - $no) * 100);
  10.     $hundred = null;
  11.     $digits_1 = strlen($no);
  12.     $i = 0;
  13.     $str = array();
  14.     $words = array('0' => '', '1' => 'one', '2' => 'two',
  15.         '3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six',
  16.         '7' => 'seven', '8' => 'eight', '9' => 'nine',
  17.         '10' => 'ten', '11' => 'eleven', '12' => 'twelve',
  18.         '13' => 'thirteen', '14' => 'fourteen',
  19.         '15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
  20.         '18' => 'eighteen', '19' =>'nineteen', '20' => 'twenty',
  21.         '30' => 'thirty', '40' => 'forty', '50' => 'fifty',
  22.         '60' => 'sixty', '70' => 'seventy',
  23.         '80' => 'eighty', '90' => 'ninety');
  24.     $digits = array('', 'hundred', 'thousand', 'lakh', 'crore');
  25.  
  26.     while ($i < $digits_1)
  27.     {
  28.         $divider = ($i == 2) ? 10 : 100;
  29.         $number = floor($no % $divider);
  30.         $no = floor($no / $divider);
  31.         $i += ($divider == 10) ? 1 : 2;
  32.         if($number)
  33.         {
  34.             $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
  35.             $hundred = ($counter == 1 && $str[0]) ? '' : null;
  36.             $str [] = ($number < 21) ? $words[$number] .
  37.             " " . $digits[$counter] . $plural . " " . $hundred
  38.             :
  39.             $words[floor($number / 10) * 10]
  40.             . " " . $words[$number % 10] . " "
  41.             . $digits[$counter] . $plural . " " . $hundred;
  42.         } else $str[] = null;
  43.     }
  44.  
  45.     $str = array_reverse($str);
  46.     $result = implode('', $str);
  47.  
  48.     if($point > 20) {
  49.         $points = ($point) ?
  50.         "" . $words[floor($point / 10) * 10] . " " .
  51.         $words[$point = $point % 10] : '';
  52.     }else{
  53.         $points = $words[$point];
  54.     }
  55.  
  56.     if($points != ''){        
  57.         echo ucwords($result) . "Rupees And " . ucwords($points) . " Paisa Only";
  58.     }else{
  59.         echo ucwords($result) . "Rupees Only";
  60.     }
  61. }
  62.  
  63. // Support Point After Two Digit Only :: https://3v4l.org/rFvbJ
  64. function displaywords($number)
  65. {
  66.     $words = array('0' => '', '1' => 'one', '2' => 'two',
  67.         '3' => 'three', '4' => 'four', '5' => 'five', '6' => 'six',
  68.         '7' => 'seven', '8' => 'eight', '9' => 'nine',
  69.         '10' => 'ten', '11' => 'eleven', '12' => 'twelve',
  70.         '13' => 'thirteen', '14' => 'fourteen',
  71.         '15' => 'fifteen', '16' => 'sixteen', '17' => 'seventeen',
  72.         '18' => 'eighteen', '19' =>'nineteen', '20' => 'twenty',
  73.         '30' => 'thirty', '40' => 'forty', '50' => 'fifty',
  74.         '60' => 'sixty', '70' => 'seventy',
  75.         '80' => 'eighty', '90' => 'ninety');
  76.     $digits = array('', '', 'hundred', 'thousand', 'lakh', 'crore');
  77.  
  78.     $number = explode(".", $number);
  79.     $result = array("","");
  80.     $j =0;
  81.     foreach($number as $val){
  82.             // loop each part of number, right and left of dot
  83.         for($i=0;$i<strlen($val);$i++){
  84.                 // look at each part of the number separately  [1] [5] [4] [2]  and  [5] [8]
  85.  
  86.                 $numberpart = str_pad($val[$i], strlen($val)-$i, "0", STR_PAD_RIGHT); // make 1 => 1000, 5 => 500, 4 => 40 etc.
  87.                 if($numberpart <= 20){
  88.                     $numberpart = 1*substr($val, $i,2);
  89.                     $i++;
  90.                     $result[$j] .= $words[$numberpart] ." ";
  91.                 }else{
  92.                     //echo $numberpart . "<br>\n"; //debug
  93.                     if($numberpart > 90){  // more than 90 and it needs a $digit.
  94.                         $result[$j] .= $words[$val[$i]] . " " . $digits[strlen($numberpart)-1] . " ";
  95.                     }else if($numberpart != 0){ // don't print zero
  96.                     $result[$j] .= $words[str_pad($val[$i], strlen($val)-$i, "0", STR_PAD_RIGHT)] ." ";
  97.                 }
  98.             }
  99.         }
  100.         $j++;
  101.     }
  102.     if(trim($result[0]) != "") echo $result[0] . "Rupees ";
  103.     if($result[1] != "") echo 'AND '.$result[1] . "Paise";
  104.     echo " Only";
  105. }
  106.    
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement