Skip to content

Instantly share code, notes, and snippets.

@jerry-maheswara-github
Last active July 18, 2018 14:54
Show Gist options
  • Save jerry-maheswara-github/44fc2c200d3c2cf955da8159438b5ea8 to your computer and use it in GitHub Desktop.
Save jerry-maheswara-github/44fc2c200d3c2cf955da8159438b5ea8 to your computer and use it in GitHub Desktop.
<?php
/************/
/************/
function terbilang_tmp($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 = terbilang_tmp($nilai - 10) . " belas";
} else if ($nilai < 100) {
$temp = terbilang_tmp($nilai / 10) . " puluh" . terbilang_tmp($nilai % 10);
} else if ($nilai < 200) {
$temp = " seratus" . terbilang_tmp($nilai - 100);
} else if ($nilai < 1000) {
$temp = terbilang_tmp($nilai / 100) . " ratus" . terbilang_tmp($nilai % 100);
} else if ($nilai < 2000) {
$temp = " seribu" . terbilang_tmp($nilai - 1000);
}
return $temp;
}
/************/
function terbilang($numbers, $opn = false)
{
if (strlen($numbers) <= 36) {
$nolnya = array('', 'ribu', 'juta', 'milyar', 'triliun', 'kuadriliun', 'kuantiliun', 'sekstiliun', 'septiliun', 'oktiliun', 'noniliun', 'desiliun');
if ($numbers >= 2000) {
$numbers = strrev($numbers);
$len = str_split($numbers, 3);
krsort($len);
$rev = array();
foreach ($len as $key => $value) {
$spelling = terbilang_tmp(strrev($value));
if ($value > 0) {
$rev[] = str_replace(' ', ' ', trim($spelling . ' ' . $nolnya[$key]));
}
}
} else {
$rev[] = terbilang_tmp($numbers);
}
if ($opn) {
echo'<pre>';print_r($rev);exit();
}
$res = str_replace(' ', ' ', trim(implode(' ', $rev)));
} else {
$res = 'no spelling available';
}
return $res;
}
/************/
//// contoh penerapan:
$x = '1001001001001001001001001001001000'; // format string, karena integer sudah tidak mampu lagi
$xx = strrev($x);
echo strrev(wordwrap($xx, 3, ",", true));
echo '<hr>';
// echo number_format($x); /// number_format tidak mampu menampung angka integer terlalu panjang
// echo '<hr>';
echo strtolower(terbilang($x,false));//exit();
echo '<hr>';
echo strtolower(terbilang($x,true));exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment