Terbilang

Mencuri Meminjam ide dari sini, berikut fungsi yang sama dalam PHP.

<?php

function sayNumber($number) {

    if (($number<0 OR $number>9999999) OR !is_int($number)) {
        $sayNumber = "belum terdefinisikan";
    }
    else {

        $dict = array (
                1 => 'se',
                2 => 'dua ',
                3 => 'tiga ',
                4 => 'empat ',
                5 => 'lima ',
                6 => 'enam ',
                7 => 'tujuh ',
                8 => 'delapan ',
                9 => 'sembilan ');

        $sayNumber = '';

        if ($number==0) {
            $sayNumber = "nol";
        }

        if ($number>=1000000 AND $number<=9999999) {

            if ($number>=1000000 AND $number<=1999999) {
                $sayNumber = $sayNumber . "satu ";
            }
            if ($number>=2000000 AND $number<=9999999) {
                $sayNumber = $sayNumber . $dict[floor($number/1000000)];
            }
            $sayNumber = $sayNumber . "juta ";
            $number = $number - (floor($number/1000000) * 1000000);
        }

        if ($number>=10000 AND $number<=999999) {
            if ($number>=100000 AND $number<=999999) {
                $sayNumber = $sayNumber . $dict[floor($number/100000)] . "ratus ";
                $number = $number - (floor($number/100000) * 100000);
            }
            if (($number>=20000 AND $number<=99999)
                    OR ($number>=10000 AND $number<=10999)) {
                $sayNumber = $sayNumber . $dict[floor($number/10000)] . "puluh ";
                $number = $number - (floor($number/10000) * 10000);
            }
            if ($number>=11000 AND $number<=19999) {
                $sayNumber = $sayNumber . $dict[($number-10000)/1000] . "belas ";
            }
            if ($number>=2000 AND $number<=9999) {
                $sayNumber = $sayNumber . $dict[floor($number/1000)];
            }
            if ($number>=1000 AND $number<=1999) {
                $sayNumber = $sayNumber . "satu ";
            }
            $sayNumber = $sayNumber . "ribu ";
            $number = $number - (floor($number/1000) * 1000);
        }

        if ($number>=1000 AND $number<=9999) {
            $sayNumber = $sayNumber . $dict[floor($number/1000)] . "ribu ";
            $number = $number - (floor($number/1000) * 1000);
        }

        if ($number>=100 AND $number<=999) {
            $sayNumber = $sayNumber . $dict[floor($number/100)] . "ratus ";
            $number = $number - (floor($number/100) * 100);
        }
        if (($number>=20 AND $number<=99) OR $number==10) {
            $sayNumber = $sayNumber . $dict[floor($number/10)] . "puluh ";
            $number = $number - (floor($number/10) * 10);
        }
        if ($number>=11 AND $number<=19) {
            $sayNumber = $sayNumber . $dict[$number-10] . "belas";
        }
        if ($number>=2 AND $number<=9) {
            $sayNumber = $sayNumber . $dict[$number];
        }
        if ($number==1) {
            $sayNumber = $sayNumber . "satu";
        }

    }

    return $sayNumber;

}
?>

Dan contoh cara pakainya:

<?php
$input = 1111111;
echo "angka = " . $input . "<br />";
echo "terbilang : ";
echo sayNumber($input);
?>

Hasil yang seharusnya muncul:

angka = 1111111
terbilang : satu juta seratus sebelas ribu seratus sebelas

Anda menemukan kesalahan? Atau bisa membuat algoritma yang lebih sederhana? Atau mau sekedar menanggapi? Saya tunggu komentarnya 😉

Published by Eric Gunawan

Happiness Engineer. WordPress Ambassador. Remote Worker. Soccer News Follower. Movie Lover. Proud Father. Lucky Husband.

9 thoughts on “Terbilang

  1. wow.. posting juga di WP saya dunk.. 😉
    sepertinya ide nya bagus nih.. saya buat pake bahasa A.. Pak Eric buat pake PHP..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: