Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Number in English Words (Indian format) php

<?php 

$obj=new IndianCurrency(12345.67);
echo $obj->get_words();

//Convert Number to Indian Currency Format
class IndianCurrency{

  public function __construct($amount){
    $this->amount=$amount;
    $this->hasPaisa=false;
    $arr=explode(".",$this->amount);
    $this->rupees=$arr[0];
    if(isset($arr[1])&&((int)$arr[1])>0){
      if(strlen($arr[1])>2){
        $arr[1]=substr($arr[1],0,2);
      }
      $this->hasPaisa=true;
      $this->paisa=$arr[1];
    }
  }
  
  public function get_words(){
    $w="";
    $crore=(int)($this->rupees/10000000);
    $this->rupees=$this->rupees%10000000;
    $w.=$this->single_word($crore,"Cror ");
    $lakh=(int)($this->rupees/100000);
    $this->rupees=$this->rupees%100000;
    $w.=$this->single_word($lakh,"Lakh ");
    $thousand=(int)($this->rupees/1000);
    $this->rupees=$this->rupees%1000;
    $w.=$this->single_word($thousand,"Thousand  ");
    $hundred=(int)($this->rupees/100);
    $w.=$this->single_word($hundred,"Hundred ");
    $ten=$this->rupees%100;
    $w.=$this->single_word($ten,"");
    $w.="Rupees ";
    if($this->hasPaisa){
      if($this->paisa[0]=="0"){
        $this->paisa=(int)$this->paisa;
      }
      else if(strlen($this->paisa)==1){
        $this->paisa=$this->paisa*10;
      }
      $w.=" and ".$this->single_word($this->paisa," Paisa");
    }
    return $w." Only";
  }

  private function single_word($n,$txt){
    $t="";
    if($n<=19){
      $t=$this->words_array($n);
    }else{
      $a=$n-($n%10);
      $b=$n%10;
      $t=$this->words_array($a)." ".$this->words_array($b);
    }
    if($n==0){
      $txt="";
    }
    return $t." ".$txt;
  }

  private function words_array($num){
    $n=[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", 100=>"Hundred",];
    return $n[$num];
  }
}
?>
Comment

PREVIOUS NEXT
Code Example
Php :: Laravel polimorfic faker 
Php :: trim string in php codeigniter 
Php :: get first cat php wp 
Php :: Query without chaining not working - Laravel 
Php :: php partisan run backup run 
Php :: laravel-websockets 403 forbidden error 
Php :: laravel gigapay list invoice 
Php :: Add text below product title on archive page + ACF 
Php :: set additional params to form laravel 
Php :: Bundling data mvc php 
Php :: laravel count 
Php :: Josn_encode php api encoding issue 
Php :: remove a specific element from array inside a loop php 
Php :: mysql.service: Start request repeated too quickly 
Php :: Add ACF to single.php 
Php :: imagelib thourgh class in codeigniter 
Php :: Only Show Specific Countries In Caldera Forms Phone Field 
Php :: php pdo multiple insert 
Php :: php listen to select event 
Php :: laravel download file from AWS s3 
Php :: use app http models in laravel 8 
Php :: fetch email from url contact form 7 
Php :: php artisan tinker new record 
Php :: PHP Superglobal - $_POST 
Php :: Add Spatie provider to providers 
Php :: get index number wordpress loop 
Php :: laravel get cookie value 
Php :: Laravel Http client retry request if fail 
Php :: eager loading set limit to relationship 
Php :: laravel collect whereNotIn not working 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =