Advertisement
citstudio

Komponen Terbilang Yii2

Dec 22nd, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.91 KB | None | 0 0
  1. <?php
  2. //  /config/web.php
  3. 'components' => [
  4.     'spellingnumber' => [
  5.         'class' => '<path_to_component_class>\SpellingNumber',
  6.     ],
  7. ....
  8. ],
  9. ?>
  10.  
  11. <?php
  12.     namespace app\components\AWS;
  13.  
  14.     use Yii;
  15.     use yii\helpers\Html;
  16.     use yii\base\Component;
  17.     use yii\base\InvalidConfigException;
  18.  
  19.     class SpellingNumber extends Component
  20.     {
  21.         private $_numbers;
  22.         private $_languages;
  23.         private $_spellResult;
  24.        
  25.         public function init()
  26.         {
  27.             parent::init();
  28.         }
  29.        
  30.         public function _spell($newNumbers = "0",$newLanguages="ID")
  31.         {
  32.             $this->_languages = $newLanguages;
  33.             $this->_numbers = $newNumbers;
  34.            
  35.             if ($this->_languages == "ID")
  36.             {
  37.                 $this->_spellInBahasa();
  38.             }else{
  39.                 $this->_spellResult = $this->_spellInEnglish($this->_numbers);
  40.             }
  41.                 return $this->_spellResult;
  42.         }
  43.        
  44.         private function _spellInBahasa()
  45.         {
  46.             if($this->_numbers<0){
  47.                 $hasil = "minus ".trim($this->_convert(x));
  48.             }else{
  49.                 $poin = trim($this->_decimals($this->_numbers));
  50.                 $hasil = trim($this->_convert($this->_numbers));
  51.             }
  52.            
  53.             if($poin){
  54.                 $hasil = $hasil." koma ".$poin;
  55.             }else{
  56.                 $hasil = $hasil;
  57.             }
  58.             $this->_spellResult = $hasil;
  59.         }
  60.        
  61.         private function _spellInEnglish($number)
  62.         {
  63.             $hyphen      = '-';
  64.             $conjunction = ' and ';
  65.             $separator   = ', ';
  66.             $negative    = 'negative ';
  67.             $decimal     = ' point ';
  68.             $dictionary  = array(
  69.                 0                   => 'zero',
  70.                 1                   => 'one',
  71.                 2                   => 'two',
  72.                 3                   => 'three',
  73.                 4                   => 'four',
  74.                 5                   => 'five',
  75.                 6                   => 'six',
  76.                 7                   => 'seven',
  77.                 8                   => 'eight',
  78.                 9                   => 'nine',
  79.                 10                  => 'ten',
  80.                 11                  => 'eleven',
  81.                 12                  => 'twelve',
  82.                 13                  => 'thirteen',
  83.                 14                  => 'fourteen',
  84.                 15                  => 'fifteen',
  85.                 16                  => 'sixteen',
  86.                 17                  => 'seventeen',
  87.                 18                  => 'eighteen',
  88.                 19                  => 'nineteen',
  89.                 20                  => 'twenty',
  90.                 30                  => 'thirty',
  91.                 40                  => 'fourty',
  92.                 50                  => 'fifty',
  93.                 60                  => 'sixty',
  94.                 70                  => 'seventy',
  95.                 80                  => 'eighty',
  96.                 90                  => 'ninety',
  97.                 100                 => 'hundred',
  98.                 1000                => 'thousand',
  99.                 1000000             => 'million',
  100.                 1000000000          => 'billion',
  101.                 1000000000000       => 'trillion',
  102.                 1000000000000000    => 'quadrillion',
  103.                 1000000000000000000 => 'quintillion'
  104.             );
  105.            
  106.             if (!is_numeric($number)) {
  107.                 return false;
  108.             }
  109.            
  110.             if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
  111.                 // overflow
  112.                 trigger_error(
  113.                     '$this->_spellInEnglish only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
  114.                     E_USER_WARNING
  115.                 );
  116.                 return false;
  117.             }
  118.  
  119.             if ($number < 0) {
  120.                 return $negative . $this->_spellInEnglish(abs($number));
  121.             }
  122.            
  123.             $string = $fraction = null;
  124.            
  125.             if (strpos($number, '.') !== false) {
  126.                 list($number, $fraction) = explode('.', $number);
  127.             }
  128.            
  129.             switch (true) {
  130.                 case $number < 21:
  131.                     $string = $dictionary[$number];
  132.                     break;
  133.                 case $number < 100:
  134.                     $tens   = ((int) ($number / 10)) * 10;
  135.                     $units  = $number % 10;
  136.                     $string = $dictionary[$tens];
  137.                     if ($units) {
  138.                         $string .= $hyphen . $dictionary[$units];
  139.                     }
  140.                     break;
  141.                 case $number < 1000:
  142.                     $hundreds  = $number / 100;
  143.                     $remainder = $number % 100;
  144.                     $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
  145.                     if ($remainder) {
  146.                         $string .= $conjunction . $this->_spellInEnglish($remainder);
  147.                     }
  148.                     break;
  149.                 default:
  150.                     $baseUnit = pow(1000, floor(log($number, 1000)));
  151.                     $numBaseUnits = (int) ($number / $baseUnit);
  152.                     $remainder = $number % $baseUnit;
  153.                     $string = $this->_spellInEnglish($numBaseUnits) . ' ' . $dictionary[$baseUnit];
  154.                     if ($remainder) {
  155.                         $string .= $remainder < 100 ? $conjunction : $separator;
  156.                         $string .= $this->_spellInEnglish($remainder);
  157.                     }
  158.                     break;
  159.             }
  160.            
  161.             if (null !== $fraction && is_numeric($fraction)) {
  162.                 $string .= $decimal;
  163.                 $words = array();
  164.                 foreach (str_split((string) $fraction) as $number) {
  165.                     $words[] = $dictionary[$number];
  166.                 }
  167.                 $string .= implode(' ', $words);
  168.             }
  169.             return $string;
  170.         }
  171.        
  172.         private function _convert($numbers)
  173.         {
  174.             $numbers = abs($numbers);
  175.             $angka = array ("","satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
  176.             $temp = "";
  177.        
  178.             if($numbers < 12){
  179.                 $temp = " ".$angka[$numbers];
  180.             }else if($numbers<20){
  181.                 $temp = $this->_convert($numbers - 10)." belas";
  182.             }else if ($numbers<100){
  183.                 $temp = $this->_convert($numbers/10)." puluh". $this->_convert($numbers%10);
  184.             }else if($numbers<200){
  185.                 $temp = " seratus".$this->_convert($numbers-100);
  186.             }else if($numbers<1000){
  187.                 $temp = $this->_convert($numbers/100)." ratus".$this->_convert($numbers%100);  
  188.             }else if($numbers<2000){
  189.                 $temp = " seribu".$this->_convert($numbers-1000);
  190.             }else if($numbers<1000000){
  191.                 $temp = $this->_convert($numbers/1000)." ribu".$this->_convert($numbers%1000);  
  192.             }else if($numbers<1000000000){
  193.                 $temp = $this->_convert($numbers/1000000)." juta".$this->_convert($numbers%1000000);
  194.             }else if($numbers<1000000000000){
  195.                 $temp = $this->_convert($numbers/1000000000)." milyar".$this->_convert($numbers%1000000000);
  196.             }
  197.             return $temp;
  198.         }
  199.        
  200.         private function _decimals($numbers)
  201.         {
  202.             $str = stristr($numbers,",");
  203.             $ex = explode(',',$numbers);
  204.             if(($ex[1]/10) >= 1){
  205.                 $a = abs($ex[1]);
  206.             }
  207.             $string = array("nol", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan",   "sembilan","sepuluh", "sebelas");
  208.             $temp = "";
  209.            
  210.             $a2 = $ex[1]/10;
  211.             $pjg = strlen($str);
  212.             $i =1;
  213.            
  214.             if($a>=1 && $a< 12){  
  215.                 $temp .= " ".$string[$a];
  216.             }else if($a>12 && $a < 20){  
  217.                 $temp .= $this->_convert($a - 10)." belas";
  218.             }else if ($a>20 && $a<100){  
  219.                 $temp .= $this->_convert($a / 10)." puluh". $this->_convert($a % 10);
  220.             }else{
  221.                 if($a2<1){
  222.                     while ($i<$pjg){    
  223.                         $char = substr($str,$i,1);    
  224.                         $i++;
  225.                         $temp .= " ".$string[$char];
  226.                     }
  227.                 }
  228.             }  
  229.             return $temp;
  230.         }
  231.     }
  232. ?>
  233.  
  234. <?php
  235.     $_bilangan["ID"] = number_format("735591.30",2,',','');
  236.     $_bilangan["EN"] = '5333367.13'; ?>
  237.                
  238.     echo Yii::$app->spellingnumber->_spell($_bilangan["ID"]);
  239.     // tujuh ratus tiga puluh lima ribu lima ratus sembilan puluh satu koma tiga puluh
  240.  
  241.     echo Yii::$app->spellingnumber->_spell($_bilangan["EN"],"EN");
  242.     // five million, three hundred and thirty-three thousand, three hundred and sixty-seven point one three
  243. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement