Location: PHPKode > scripts > terbilang untuk > terbilang-untuk/Class_terbilang.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Class_terbilang {

    function tertulis($nilai)
    {
        $jml = str_split($nilai);
        $banyak = strlen($nilai);        

        $kata=''; 
        $bil1 = '';
        $bil2 = ''; 
        $bilribu1 = '';
        $bilribu2 = ''; 
        $biljuta1 = '';
        $biljuta2 = '';           
          
        $i=0; 
//        foreach($jml as $value)
//        {            
            $panjang = $banyak - $i;
            switch ($panjang):
                 case 10:
                    $biljuta1 = $this->jutaan($jml);
                    break;
                 case 9:
                    $biljuta2 = $this->jutaan($jml);
                    break;
 
            
                 case 8:
                    $biljuta1 = $this->jutaan($jml);
                    break;
                 case 7:
                    $biljuta2 = $this->jutaan($jml);
                    break;
                 case 6:
                    $kata .= $this->ribuan($jml);
                    break; 
                 case 5:
                    $bilribu1 = $this->ribuan($jml);                    
                    break;                 
                 case 4:
                    $bilribu2 = $this->ribuan($jml);
                    break;    
                 case 3:
                    $kata .= $this->ratusan($jml)." ";
                    break;
                 case 2:                    
                    $bil1 = $this->belasan($jml);
                    break;
                 case 1:
                    $bil2 = $this->satuan($jml);
                    break;                       
            endswitch;
//       }
       
       return $kata;

    }
    
    
    
   function satuan($nilai)
   {
          print $this->bunyi($nilai[0]);
   }
   
   function belasan($nilai)
   {
   	    
   	    if ($nilai[0] == 1)
   	    {
   	        if ($nilai[1] == 0) { print 'Sepuluh'; }
            else
            if ($nilai[1] == 1) { print 'Sebelas'; }                	    
            else 
            {
               $kalimat = $this->bunyi($nilai[1])."belas";
               print $kalimat;
            }   
   	    } 
   	    else
        if ($nilai[0] > 1)
   	    {
               $kalimat = $this->puluhan($nilai);
               print $kalimat;
        }
   	    
   	     
   }

   function puluhan($nilai)
   {        
        
            
       
   	   if (($nilai[0] == 0) && ($nilai[1]>0 )){
        	$kalimat =''.$this->bunyi($nilai[1]);
        	return $kalimat;
        }
        else
   	    if (($nilai[0] > 1) and ($nilai[1] == 0))
        {
            $kalimat = $this->bunyi($nilai[0])." Puluh ";
            
            return $kalimat;
        }   
        else 
        if (($nilai[0] > 1) and ($nilai[1] > 0))
        {
            $kalimat = $this->bunyi($nilai[0])." Puluh ".$this->bunyi($nilai[1]);       
            return $kalimat;        
        }  
        
        
        if (($nilai[0] == 0) and ($nilai[1]>0)){
        	$kalimat = '';
            return $kalimat;
        }
         
   }

   function ratusan($nilai)
   {
       
        $angka = array($nilai[1], $nilai[2]);
        if ($nilai[0] == 0)
        {
        	return '';
        }
        else
   	    if ($nilai[0] == 1)
        {             
        	return 'Seratus '.$this->puluhan($angka); 
        }
        else
        {
            if ($nilai > 1){
            $kalimat = $this->bunyi($nilai[0])." Ratus ".$this->puluhan($angka);;
            return $kalimat;
            }    
        }     
        
        
        
   }

   function ribuan($nilai)
   {
        $banyak = count($nilai);

        if ($banyak == 4)
        {
           $angka = array($nilai[1], $nilai[2], $nilai[3]);
           if ($nilai[0] == 0)
           {
           	  print ''; 
           }
           else
           if ($nilai[0] == 1)
           {
              print 'Seribu '.$this->ratusan($angka); 
           }
           else
           {            
        	  $kalimat = $this->bunyi($nilai[0])." Ribu ".$this->ratusan($angka);
              print $kalimat;
           }  
        }
        
        if ($banyak == 5)
        {
           if ($nilai[0] == 0)
           {
           }
           else 
           if ($nilai[0] == 1)
           {
              $angka = array($nilai[0], $nilai[1]);
           	  print $this->belasan($angka)."ribu";
           }
           else {
              $angka = array($nilai[1], $nilai[2], $nilai[3], $nilai[4]);
              print $this->bunyi($nilai[0]).'puluh ';
              if ($nilai[1] == 0){
                 print ' Ribu ';
              }
              else {
              print $this->ribuan($angka);              
              }
           }
        }   
        
        if ($banyak == 6)
        {
        	$angka  = array($nilai[0],$nilai[1],$nilai[2]);
        	$angka2 =  array($nilai[3],$nilai[4],$nilai[5]);
         	if ($this->ratusan($angka) <> '') {
         	   print $this->ratusan($angka).' Ribu '.$this->ratusan($angka2);
         	}
        	

        }   
   
   
   }
   
   
   

   function jutaan($nilai)
   {
        
      $banyak = count($nilai);
      
      if ($banyak == 9){
      	$ratusan = array($nilai[0], $nilai[1], $nilai[2]);
      	$juta = array($nilai[3],$nilai[4],$nilai[5],$nilai[6],$nilai[7],$nilai[8]); 
      	print $this->ratusan($ratusan).' Juta ';
      	if ($nilai[3] == 0){
      		$juta = array($nilai[4],$nilai[5],$nilai[6],$nilai[7],$nilai[8]); 
      		print $this->ribuan($juta);
      	}
      	else {	
      	  print $this->ribuan($juta);
      	}  
      }
      
      
      if ($banyak == 8){
      	$belasan = array($nilai[0], $nilai[1]); 
      	$juta = array($nilai[2], $nilai[3],$nilai[4],$nilai[5],$nilai[6],$nilai[7]); 
      	print $this->belasan($belasan)." Juta ";
      	print $this->ribuan($juta);      	
      	
      }
      	
      
      if ($banyak == 7){
   	  $angka = array($nilai[1], $nilai[2],$nilai[3],$nilai[4],$nilai[5],$nilai[6]);  
   	  print $this->bunyi($nilai[0])." Juta ";
   	  print $this->ribuan($angka);
      }
        
           
   }



   function bunyi($satuan)
   {
      
   	  $bunyi ='';
      switch ($satuan) :
            case '0':
                $bunyi = '';  
                break;
            case '1':
                $bunyi = 'Satu';
                break;
            case '2':
                $bunyi = 'Dua';
                break;
            case '3':
                $bunyi = 'Tiga';
                break;
            case '4':
                $bunyi = 'Empat';
                break;
            case '5':
                $bunyi = 'Lima';
                break;
            case '6':
                $bunyi = 'Enam';
                break;
             case '7':
                $bunyi = 'Tujuh';
                break;
            case '8':
                $bunyi = 'Delapan';
                break;
            case '9':
                $bunyi = 'Sembilan';
                break;            
        endswitch;
        
        
        return $bunyi; 
   }
}

?>
Return current item: terbilang untuk