Advertisement
gurumutant

Demo Fungsi Terbilang di PHP

Jan 26th, 2024 (edited)
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. <?php
  2.     /*
  3.         Demo fungsi Terbilang untuk mengetahui pembacaan bilangan besar dalam Bahasa Indonesia
  4.         Original Source : https://www.malasngoding.com/cara-mudah-membuat-fungsi-terbilang-dengan-php/
  5.         Modified to add kuadriliun,kuintiliun, and a form to test the function, suppress deprecated errors, by RPL SMKN 1 Pacitan
  6.     */
  7.  
  8.     error_reporting(E_ALL ^ E_DEPRECATED);
  9.  
  10.     function penyebut($nilai) {
  11.         $nilai = abs($nilai);
  12.         $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
  13.         $temp = "";
  14.         if ($nilai < 12) {
  15.             $temp = " ". $huruf[$nilai];
  16.         } else if ($nilai <20) {
  17.             $temp = penyebut($nilai - 10). " belas";
  18.         } else if ($nilai < 100) {
  19.             $temp = penyebut($nilai/10)." puluh". penyebut($nilai % 10);
  20.         } else if ($nilai < 200) {
  21.             $temp = " seratus" . penyebut($nilai - 100);
  22.         } else if ($nilai < 1000) {
  23.             $temp = penyebut($nilai/100) . " ratus" . penyebut($nilai % 100);
  24.         } else if ($nilai < 2000) {
  25.             $temp = " seribu" . penyebut($nilai - 1000);
  26.         } else if ($nilai < 1000000) {
  27.             $temp = penyebut($nilai/1000) . " ribu" . penyebut($nilai % 1000);
  28.         } else if ($nilai < 1000000000) {
  29.             $temp = penyebut($nilai/1000000) . " juta" . penyebut($nilai % 1000000);
  30.         } else if ($nilai < 1000000000000) {
  31.             $temp = penyebut($nilai/1000000000) . " milyar" . penyebut(fmod($nilai,1000000000));
  32.         } else if ($nilai < 1000000000000000) {
  33.             $temp = penyebut($nilai/1000000000000) . " trilyun" . penyebut(fmod($nilai,1000000000000));
  34.         } else if ($nilai < 1000000000000000000) {
  35.             $temp = penyebut($nilai/1000000000000000) . " kuadriliun" . penyebut(fmod($nilai,1000000000000000));
  36.         } else if ($nilai < 1000000000000000000000) {
  37.             $temp = penyebut($nilai/1000000000000000000) . " kuintiliun" . penyebut(fmod($nilai,1000000000000000000));
  38.         }    
  39.         return $temp;
  40.     }
  41.  
  42.     function terbilang($nilai) {
  43.         if($nilai<0) {
  44.             $hasil = "minus ". trim(penyebut($nilai));
  45.         } else {
  46.             $hasil = trim(penyebut($nilai));
  47.         }          
  48.         return $hasil;
  49.     }
  50.  
  51.     if (isset($_POST["bilangan"]) && is_numeric($_POST["bilangan"])) {
  52.         echo "<h3>".number_format($_POST["bilangan"], 0,',','.')."</h3>";
  53.         echo terbilang($_POST["bilangan"]);
  54.     }
  55. ?>
  56. <form method="post">
  57. <input type="text" name="bilangan" id="bilangan">
  58. <input type="submit" value="Cek">
  59. </form>    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement