-
-
Save eghojansu/f97d831d5212105111b0 to your computer and use it in GitHub Desktop.
Terbilang Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Terbilang | |
{ | |
public static function generate($angka) { | |
$angka = (float)$angka; | |
$bilangan = array( | |
'', | |
'satu', | |
'dua', | |
'tiga', | |
'empat', | |
'lima', | |
'enam', | |
'tujuh', | |
'delapan', | |
'sembilan', | |
'sepuluh', | |
'sebelas' | |
); | |
if ($angka < 12) { | |
return $bilangan[$angka]; | |
} else if ($angka < 20) { | |
return $bilangan[$angka - 10] . ' belas'; | |
} else if ($angka < 100) { | |
$hasil_bagi = (int)($angka / 10); | |
$hasil_mod = $angka % 10; | |
return trim(sprintf('%s puluh %s', $bilangan[$hasil_bagi], $bilangan[$hasil_mod])); | |
} else if ($angka < 200) { | |
return sprintf('seratus %s', self::generate($angka - 100)); | |
} else if ($angka < 1000) { | |
$hasil_bagi = (int)($angka / 100); | |
$hasil_mod = $angka % 100; | |
return trim(sprintf('%s ratus %s', $bilangan[$hasil_bagi], self::generate($hasil_mod))); | |
} else if ($angka < 2000) { | |
return trim(sprintf('seribu %s', self::generate($angka - 1000))); | |
} else if ($angka < 1000000) { | |
$hasil_bagi = (int)($angka / 1000); // karena hasilnya bisa ratusan jadi langsung digunakan rekursif | |
$hasil_mod = $angka % 1000; | |
return sprintf('%s ribu %s', self::generate($hasil_bagi), self::generate($hasil_mod)); | |
} else if ($angka < 1000000000) { | |
// hasil bagi bisa satuan, belasan, ratusan jadi langsung kita gunakan rekursif | |
$hasil_bagi = (int)($angka / 1000000); | |
$hasil_mod = $angka % 1000000; | |
return trim(sprintf('%s juta %s', self::generate($hasil_bagi), self::generate($hasil_mod))); | |
} else if ($angka < 1000000000000) { | |
// bilangan 'milyaran' | |
$hasil_bagi = (int)($angka / 1000000000); | |
$hasil_mod = fmod($angka, 1000000000); | |
return trim(sprintf('%s milyar %s', self::generate($hasil_bagi), self::generate($hasil_mod))); | |
} else if ($angka < 1000000000000000) { | |
// bilangan 'triliun' | |
$hasil_bagi = $angka / 1000000000000; | |
$hasil_mod = fmod($angka, 1000000000000); | |
return trim(sprintf('%s triliun %s', self::generate($hasil_bagi), self::generate($hasil_mod))); | |
} else { | |
return 'Wow...'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment