# Function Terbilang for PHP

I have a software application developing project. My boss asked me to added new features. One of the features is about translating money number to be words. Example: “Rp. 125.000” is converted to “Seratus Dua Puluh Lima Ribu Rupiah”.

Well, this time I’m so lazy to make the algortithm from scratch. So i decide to googling it. I found one here. It’s written using Java programming language and limited only to millions rupiah. I convert that code to PHP and make its limit up to trillion rupiah.

So this is the code:

[code language=”php”]
function kata(\$num) {
\$angka = array("","satu","dua","tiga","empat","lima","enam","tujuh","delapan","sembilan","sepuluh","sebelas");
\$temp = "";

\$num = (float) \$num;

if(\$num < 12){ // satuan sampai 12
\$temp = \$angka[\$num]." ";
}
else if(\$num < 20){ // belasan
\$temp = kata(\$num-10)." belas ";
}
else if(\$num < 100){ // puluhan
\$temp = kata(\$num/10)." puluh ".kata(\$num%10)." ";
}
else if(\$num < 200){ // ratusan tapi dibawah 200
\$temp = "seratus ".kata(\$num-100)." ";
}
else if(\$num < 1000){ // ratusan
\$temp = kata(\$num/100)." ratus ".kata(\$num%100)." ";
}
else if(\$num < 1000000){ // ribuan
\$temp = kata(\$num/1000)." ribu ".kata(\$num%1000)." ";
}
else if(\$num < 1000000000){ // jutaan
\$temp = kata(\$num/1000000)." juta ".kata(\$num%1000000)." ";
}
else if(\$num < 1000000000000){ // miliaran
\$temp = kata(\$num/1000000000)." miliar ".kata(\$num%1000000000)." ";
}
else if(\$num < 1000000000000000){ // triliunan
\$temp = kata(\$num/1000000000099)." triliun ".kata(\$num%1000000000000)." ";
}
return \$temp;
}

echo ucwords(kata(‘195000’)."rupiah");
[/code]

Oh yeah, you can run that php code here.

## One thought on “Function Terbilang for PHP”

This site uses Akismet to reduce spam. Learn how your comment data is processed.