Advertisement
bangnaga

PHP Terbilang

May 27th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.92 KB | None | 0 0
  1. <?php
  2. function konversi($x){
  3.    
  4.   $x = abs($x);
  5.   $angka = array ("","satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
  6.   $temp = "";
  7.    
  8.   if($x < 12){
  9.    $temp = " ".$angka[$x];
  10.   }else if($x<20){
  11.    $temp = konversi($x - 10)." belas";
  12.   }else if ($x<100){
  13.    $temp = konversi($x/10)." puluh". konversi($x%10);
  14.   }else if($x<200){
  15.    $temp = " seratus".konversi($x-100);
  16.   }else if($x<1000){
  17.    $temp = konversi($x/100)." ratus".konversi($x%100);  
  18.   }else if($x<2000){
  19.    $temp = " seribu".konversi($x-1000);
  20.   }else if($x<1000000){
  21.    $temp = konversi($x/1000)." ribu".konversi($x%1000);  
  22.   }else if($x<1000000000){
  23.    $temp = konversi($x/1000000)." juta".konversi($x%1000000);
  24.   }else if($x<1000000000000){
  25.    $temp = konversi($x/1000000000)." milyar".konversi($x%1000000000);
  26.   }
  27.    
  28.   return $temp;
  29.  }
  30.    
  31.  function tkoma($x){
  32.   $str = stristr($x,",");
  33.   $ex = explode(',',$x);
  34.    
  35.   if(($ex[1]/10) >= 1){
  36.    $a = abs($ex[1]);
  37.   }
  38.   $string = array("nol", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan",   "sembilan","sepuluh", "sebelas");
  39.   $temp = "";
  40.  
  41.   $a2 = $ex[1]/10;
  42.   $pjg = strlen($str);
  43.   $i =1;
  44.      
  45.    
  46.   if($a>=1 && $a< 12){  
  47.    $temp .= " ".$string[$a];
  48.   }else if($a>12 && $a < 20){  
  49.    $temp .= konversi($a - 10)." belas";
  50.   }else if ($a>20 && $a<100){  
  51.    $temp .= konversi($a / 10)." puluh". konversi($a % 10);
  52.   }else{
  53.    if($a2<1){
  54.      
  55.     while ($i<$pjg){    
  56.      $char = substr($str,$i,1);    
  57.      $i++;
  58.      $temp .= " ".$string[$char];
  59.     }
  60.    }
  61.   }  
  62.   return $temp;
  63.  }
  64.  
  65.  function terbilang($x){
  66.   if($x<0){
  67.    $hasil = "minus ".trim(konversi(x));
  68.   }else{
  69.    $poin = trim(tkoma($x));
  70.    $hasil = trim(konversi($x));
  71.   }
  72.    
  73. if($poin){
  74.    $hasil = $hasil." koma ".$poin;
  75.   }else{
  76.    $hasil = $hasil;
  77.   }
  78.   return $hasil;  
  79.  }
  80.  
  81. echo ucwords(terbilang('3,99'));
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement