Skip to content

Instantly share code, notes, and snippets.

@amarullz
Last active May 14, 2022
Embed
What would you like to do?
Library Terbilang dalam PHP

Library Terbilang dalam PHP

Informasi lengkap dapat dibaca di: https://amarullz.com/2022/04/27/php-library-terbilang-untuk-bukti-pembayaran-atau-kwitansi-pada-aplikasi/

License

Copyright 2022 Ahmad Amarullah (https://amarullz.com)

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

<?php
include "libterbilang.php"; /* include Class LibTerbilang */
/* nilai yang akan dibuatkan terbilang */
$a = 5478200;
echo "Terbilang untuk {$a} = " . (LibTerbilang::terbilang($a));
/* nilai naru */
$a = "83956712400";
/* Gunakan ucwords untuk kapital tiap kata */
$kalimat = ucwords(LibTerbilang::terbilang($a));
echo "Anda membayar sebesar: {$kalimat} Rupiah";
<?php
/*
* Copyright 2022 Ahmad Amarullah (https://amarullz.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
class LibTerbilang
{
private static function charAt($s, $i)
{
return substr($s, $i, 1);
}
private static function getValidityNaN($str, $from, $to, $min = 1, $max = 9)
{
$val = false;
$from = ($from < 0) ? 0 : $from;
for ($i = $from; $i < $to; $i++) {
if (((int) LibTerbilang::charAt($str, $i) >= $min) && ((int) LibTerbilang::charAt($str, $i) <= $max)) $val = true;
}
return $val;
}
private static function getTerbilang($i, $str, $len)
{
$numA = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan");
$numB = array("", "se", "dua ", "tiga ", "empat ", "lima ", "enam ", "tujuh ", "delapan ", "sembilan ");
$numC = array("", "satu ", "dua ", "tiga ", "empat ", "lima ", "enam ", "tujuh ", "delapan ", "sembilan ");
$numD = array(0 => "puluh", 1 => "belas", 2 => "ratus", 4 => "ribu", 7 => "juta", 10 => "milyar", 13 => "triliun");
$buf = "";
$pos = $len - $i;
switch ($pos) {
case 1:
if (!LibTerbilang::getValidityNaN($str, $i - 1, $i, 1, 1))
$buf = $numA[(int) LibTerbilang::charAt($str, $i)];
break;
case 2:
case 5:
case 8:
case 11:
case 14:
if ((int) LibTerbilang::charAt($str, $i) == 1) {
if ((int) LibTerbilang::charAt($str, $i + 1) == 0)
$buf = ($numB[(int) LibTerbilang::charAt($str, $i)]) . ($numD[0]);
else
$buf = ($numB[(int) LibTerbilang::charAt($str, $i + 1)]) . ($numD[1]);
} else if ((int) LibTerbilang::charAt($str, $i) > 1) {
$buf = ($numB[(int) LibTerbilang::charAt($str, $i)]) . ($numD[0]);
}
break;
case 3:
case 6:
case 9:
case 12:
case 15:
if ((int) LibTerbilang::charAt($str, $i) > 0) {
$buf = ($numB[(int) LibTerbilang::charAt($str, $i)]) . ($numD[2]);
}
break;
case 4:
case 7:
case 10:
case 13:
if (LibTerbilang::getValidityNaN($str, $i - 2, $i)) {
if (!LibTerbilang::getValidityNaN($str, $i - 1, $i, 1, 1))
$buf = $numC[(int) LibTerbilang::charAt($str, $i)] . ($numD[$pos]);
else
$buf = $numD[$pos];
} else if ((int) LibTerbilang::charAt($str, $i) > 0) {
if ($pos == 4)
$buf = ($numB[(int) LibTerbilang::charAt($str, $i)]) . ($numD[$pos]);
else
$buf = ($numC[(int) LibTerbilang::charAt($str, $i)]) . ($numD[$pos]);
}
break;
}
return $buf;
}
/**
* Dapatkan nilai terbilang dari suatu bilangan
* Contoh:
* $val = LibTerbilang::terbilang("2000");
* menghasilkan: dua ribu
*
* @param string $nominal Bilangan yang akan dibaca
* @return string String Terbilang
*/
public static function terbilang($nominal)
{
$buf = "";
$str = $nominal . "";
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$buf = trim($buf) . " " . (LibTerbilang::getTerbilang($i, $str, $len));
}
return trim($buf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment