Created
February 10, 2021 13:22
-
-
Save abdasis/e0623ca82c03ea92533748950e79431c to your computer and use it in GitHub Desktop.
Helper untuk membuat angka terbilang di Laravel
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 | |
{ | |
function penyebut($nilai) { | |
$nilai = abs($nilai); | |
$huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas"); | |
$temp = ""; | |
if ($nilai < 12) { | |
$temp = " ". $huruf[$nilai]; | |
} else if ($nilai <20) { | |
$temp = penyebut($nilai - 10). " belas"; | |
} else if ($nilai < 100) { | |
$temp = penyebut($nilai/10)." puluh". penyebut($nilai % 10); | |
} else if ($nilai < 200) { | |
$temp = " seratus" . penyebut($nilai - 100); | |
} else if ($nilai < 1000) { | |
$temp = penyebut($nilai/100) . " ratus" . penyebut($nilai % 100); | |
} else if ($nilai < 2000) { | |
$temp = " seribu" . penyebut($nilai - 1000); | |
} else if ($nilai < 1000000) { | |
$temp = penyebut($nilai/1000) . " ribu" . penyebut($nilai % 1000); | |
} else if ($nilai < 1000000000) { | |
$temp = penyebut($nilai/1000000) . " juta" . penyebut($nilai % 1000000); | |
} else if ($nilai < 1000000000000) { | |
$temp = penyebut($nilai/1000000000) . " milyar" . penyebut(fmod($nilai,1000000000)); | |
} else if ($nilai < 1000000000000000) { | |
$temp = penyebut($nilai/1000000000000) . " trilyun" . penyebut(fmod($nilai,1000000000000)); | |
} | |
return $temp; | |
} | |
function terbilang($nilai) { | |
if($nilai<0) { | |
$hasil = "minus ". trim(penyebut($nilai)); | |
} else { | |
$hasil = trim(penyebut($nilai)); | |
} | |
return $hasil; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment