Skip to content

Instantly share code, notes, and snippets.

@khaeransori
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save khaeransori/e9636ef77aaaf39d88c0 to your computer and use it in GitHub Desktop.

Select an option

Save khaeransori/e9636ef77aaaf39d88c0 to your computer and use it in GitHub Desktop.
Fungsi Mencari Bilangan Terbilang
<?php
function kekata($x) {
$x = abs($x);
$angka = array("", "satu", "dua", "tiga", "empat", "lima",
"enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
$temp = "";
if ($x <12) {
$temp = " ". $angka[$x];
} else if ($x <20) {
$temp = kekata($x - 10). " belas";
} else if ($x <100) {
$temp = kekata($x/10)." puluh". kekata($x % 10);
} else if ($x <200) {
$temp = " seratus" . kekata($x - 100);
} else if ($x <1000) {
$temp = kekata($x/100) . " ratus" . kekata($x % 100);
} else if ($x <2000) {
$temp = " seribu" . kekata($x - 1000);
} else if ($x <1000000) {
$temp = kekata($x/1000) . " ribu" . kekata($x % 1000);
} else if ($x <1000000000) {
$temp = kekata($x/1000000) . " juta" . kekata($x % 1000000);
} else if ($x <1000000000000) {
$temp = kekata($x/1000000000) . " milyar" . kekata(fmod($x,1000000000));
} else if ($x <1000000000000000) {
$temp = kekata($x/1000000000000) . " trilyun" . kekata(fmod($x,1000000000000));
}
return $temp;
}
function terbilang($x, $style=4) {
if($x<0) {
$hasil = "minus ". trim(kekata($x));
} else if ($x<1) {
$hasil = "nol";
} else {
$hasil = trim(kekata($x));
}
switch ($style) {
case 1:
$hasil = strtoupper($hasil);
break;
case 2:
$hasil = strtolower($hasil);
break;
case 3:
$hasil = ucwords($hasil);
break;
default:
$hasil = ucfirst($hasil);
break;
}
return $hasil;
}
?>
<?php
if (isset($_GET['aksi'])) {
$aksi = $_GET['aksi'] ;
switch ($aksi) {
case 'hitung':
$angka = $_POST['angka'];
$hasil_terbilang = terbilang($angka);
echo 'Terbilangnya adalah ' . $hasil_terbilang;
echo '<br />';
break;
}
}
?>
<html>
<head>
<title>Terbilang</title>
</head>
<body>
<form action="?aksi=hitung" method="post">
<?php
echo 'Masukkan suatu angka ! <br/>';
echo 'Angka = '
?>
<input type="number" name="angka" value="<?php echo isset($angka) ? $angka : ''; ?>"> <br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment