143 lines
4.2 KiB
PHP
143 lines
4.2 KiB
PHP
<?php
|
|
|
|
/*******************************************************************
|
|
|
|
Module : /Web/Terbilang.php
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : February 12th, 2009.
|
|
Last Modified : August 22nd, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Webapp\Web;
|
|
|
|
/**
|
|
* Class Terbilang
|
|
*
|
|
* Pembuatan text terbilang.
|
|
*
|
|
* @license MIT, https://www.3fonia.com
|
|
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
|
*
|
|
* @package Webapp\Web
|
|
*/
|
|
|
|
final class Terbilang {
|
|
private $numb = ["","Satu","Dua","Tiga","Empat","Lima","Enam","Tujuh","Delapan","Sembilan"];
|
|
|
|
public function __construct() {}
|
|
|
|
public function Terbilang(float $nilai=0, string $muang="Rupiah"): string {
|
|
$number = 0;
|
|
$hasil = "";
|
|
$int = (string)$nilai;
|
|
if (is_int(strpos($int,"-"))) {
|
|
$number = substr($int,strpos($int,"-")+1,strlen($int));
|
|
$hasil = "Minus";
|
|
} else {
|
|
$number = $int;
|
|
}
|
|
|
|
# strpost : untuk mencari "." pada variabel number
|
|
$xpos = strpos($number,".");
|
|
if (is_int($xpos)) {
|
|
# round : Untuk pembulatan angka desimal, strlen : untuk menghitung jumlah karakter
|
|
$pecahan = round((float)substr($number,$xpos,strlen($number)),2);
|
|
$last = substr($number,0,$xpos);
|
|
} else {
|
|
$pecahan = "";
|
|
$last = (float)$number;
|
|
}
|
|
if ($last==0 || $number==0) {
|
|
return $hasil."NOL ".$muang;
|
|
} else {
|
|
# floor : Pembulatan angka terbaik dari desimal, pow : Ekspresi Eksponensial
|
|
$triliun = floor($last/pow(10,12));
|
|
$last = $this->mod($last,1000000000000);
|
|
|
|
$miliar = floor($last/pow(10,9));
|
|
$last = $this->mod($last,1000000000);
|
|
|
|
$juta = floor($last/pow(10,6));
|
|
$last = $this->mod($last,1000000);
|
|
|
|
$ribu = floor($last/pow(10,3));
|
|
$last = $this->mod($last,1000);
|
|
|
|
$kata = $this->tigadigit($triliun, "Triliun");
|
|
$kata .= $this->tigadigit($miliar, "Miliar");
|
|
$kata .= $this->tigadigit($juta, "Juta");
|
|
$kata .= $this->tigadigit($ribu, "Ribu");
|
|
$kata .= $this->tigadigit($last, "");
|
|
$kata .= " ".$muang;
|
|
}
|
|
if ($pecahan>0) {
|
|
$kata .= " Dan". $this->tigadigit(round($pecahan*100),"Sen");
|
|
}
|
|
return $hasil . $kata;
|
|
}
|
|
|
|
private function mod(float $a, float $b): float {
|
|
return $a-$b*floor($a/$b);
|
|
}
|
|
|
|
private function tigadigit(float $amount, string $suffix=""): string {
|
|
$last = (int)$amount;
|
|
$kata = "";
|
|
if ($last == 1 && $suffix=="Ribu") {
|
|
$kata = " Seribu";
|
|
return $kata;
|
|
}
|
|
if ($last < 20 && $last > 10) {
|
|
if ($last==11) {
|
|
$kata = " Sebelas";
|
|
} else {
|
|
$kata = " ".$this->numb[$last-10]." Belas";
|
|
}
|
|
if ($suffix != "") {
|
|
$kata .= " ". $suffix;
|
|
}
|
|
return $kata;
|
|
}
|
|
$ratus = floor($last/100);
|
|
if ($ratus <= 0) {
|
|
$kata .= "";
|
|
} elseif ($ratus == 1) {
|
|
$kata .= " Seratus";
|
|
} else {
|
|
$kata .= " ".$this->numb[$ratus]." Ratus";
|
|
}
|
|
$last = $this->mod($last,100);
|
|
if ($last < 20 && $last > 10) {
|
|
if ($last == 11) {
|
|
$kata .= " Sebelas ". $suffix;
|
|
} else {
|
|
$kata .= " ".$this->numb[$last-10]." Belas ". $suffix;
|
|
}
|
|
return $kata;
|
|
}
|
|
$puluh = floor($last/10);
|
|
if ($puluh == 0) {
|
|
$kata .= "";
|
|
} elseif ($puluh == 1) {
|
|
$kata .= " Sepuluh";
|
|
} else {
|
|
$kata .= " ".$this->numb[$puluh]." Puluh";
|
|
}
|
|
$last = $this->mod($last,10);
|
|
if ($last>0 && $last<=9) {
|
|
$kata .= " ".$this->numb[$last];
|
|
}
|
|
if ($amount>0 && $amount<=1000) {
|
|
$kata .= " ".$suffix;
|
|
}
|
|
return $kata;
|
|
}
|
|
}
|
|
|
|
?>
|