Home about IT Motivation Course Sales Project About Me

Monday, January 26, 2009

konversi angka ke bahasa di php

berikut script untuk membuat angka ke dalam bahasa:

ini yang di pdfnya

$input = trim($data1['sum_sspcp']);
$hasil = terbilang($input,$style=4,$strcomma=",");
$pdf->SetleftMargin(50);
$pdf->SetXY(50,200);
$pdf->write(4,$hasil);
$pdf->write(4,' rupiah.');


ini yang di class nya:
<?php
// by irfani.firdausy.com
function ctword($x) {
$x = abs($x);
$number = array("", "satu", "dua", "tiga", "empat", "lima","enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
$temp = "";

if ($x <12) {
$temp = " ". $number[$x];
} else if ($x <20) {
$temp = ctword($x - 10). " belas";
} else if ($x <100) {
$temp = ctword($x/10)." puluh". ctword($x % 10);
} else if ($x <200) {
$temp = " seratus" . ctword($x - 100);
} else if ($x <1000) {
$temp = ctword($x/100) . " ratus" . ctword($x % 100);
} else if ($x <2000) {
$temp = " seribu" . ctword($x - 1000);
} else if ($x <1000000) {
$temp = ctword($x/1000) . " ribu" . ctword($x % 1000);
} else if ($x <1000000000) {
$temp = ctword($x/1000000) . " juta" . ctword($x % 1000000);
} else if ($x <1000000000000) {
$temp = ctword($x/1000000000) . " milyar" . ctword(fmod($x,1000000000));
} else if ($x <1000000000000000) {
$temp = ctword($x/1000000000000) . " trilyun" . ctword(fmod($x,1000000000000));
}
return $temp;
}
function terbilang($x,$style=4,$strcomma=",") {
if($x<0) {
$result = "minus ". trim(ctword($x));
} else {
$arrnum=explode("$strcomma",$x);
$arrcount=count($arrnum);
if ($arrcount==1){
$result = trim(ctword($x));
}else if ($arrcount>1){
$result = trim(ctword($arrnum[0])) . " koma " . trim(ctword($arrnum[1]));
}
}
switch ($style) {
case 1: //1=uppercase dan
$result = strtoupper($result);
break;
case 2: //2= lowercase
$result = strtolower($result);
break;
case 3: //3= uppercase on first letter for each word
$result = ucwords($result);
break;
default: //4= uppercase on first letter
$result = ucfirst($result);
break;
}
return $result;
}
?>

No comments: