Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Last active May 10, 2021 09:47
Show Gist options
  • Save fzn0x/8e813a630c6e081ddb72919be52fdbdc to your computer and use it in GitHub Desktop.
Save fzn0x/8e813a630c6e081ddb72919be52fdbdc to your computer and use it in GitHub Desktop.
PHP Programming Number To Words - Indonesia
/**
*
* (C) 2020 Muhammad Fauzan
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See the GNU General Public License: <http://www.gnu.org/licenses/>.
*
*/
/**
* Number To Words (ID)
*
* @param integer $number
* @return string
*/
function numberToWordsID($number)
{
$max_size = pow(10,18);
if (!$number) return "nol";
$suffix = ""; //add suffix to fix scope problem
if (is_int($number) && $number < abs($max_size))
{
switch ($number)
{
case $number < 0:
$prefix = "negatif";
$suffix = numberToWordsID(-1*$number);
$string = $prefix . " " . $suffix;
break;
case 1:
$string = "satu";
break;
case 2:
$string = "dua";
break;
case 3:
$string = "tiga";
break;
case 4:
$string = "empat";
break;
case 5:
$string = "lima";
break;
case 6:
$string = "enam";
break;
case 7:
$string = "tujuh";
break;
case 8:
$string = "delapan";
break;
case 9:
$string = "sembilan";
break;
case 10:
$string = "sepuluh";
break;
case 11:
$string = "sebelas";
break;
case 12:
$string = "dua belas";
break;
case 13:
$string = "tiga belas";
break;
case 15:
$string = "lima belas";
break;
case $number < 20:
$string = numberToWordsID($number%10);
if ($number == 18)
{
$suffix = " belas";
} else
{
$suffix = " belas";
}
$string .= $suffix;
break;
case 20:
$string = "dua puluh";
break;
case 30:
$string = "tiga puluh";
break;
case 40:
$string = "empat puluh";
break;
case 50:
$string = "lima puluh";
break;
case 60:
$string = "enam puluh";
break;
case 70:
$string = "tujuh puluh";
break;
case 80:
$string = "delapan puluh";
break;
case 90:
$string = "sembilan puluh";
break;
case $number < 100:
$prefix = numberToWordsID($number-$number%10);
$suffix = numberToWordsID($number%10);
$string = $prefix . "-" . $suffix;
break;
case $number < pow(10,3):
$getNumber = numberToWordsID(intval(floor($number/pow(10,2))));
$getNumber = $getNumber == "satu" ? "se" : $getNumber . " ";
$prefix = $getNumber . "ratus";
if ($number%pow(10,2)) $suffix = " " . numberToWordsID($number%pow(10,2));
$string = $prefix . $suffix;
break;
case $number < pow(10,6):
$prefix = numberToWordsID(intval(floor($number/pow(10,3)))) . " ribu";
if ($number%pow(10,3)) $suffix = numberToWordsID($number%pow(10,3));
$string = $prefix . " " . $suffix;
break;
case $number < pow(10,9):
$prefix = numberToWordsID(intval(floor($number/pow(10,6)))) . " juta";
if ($number%pow(10,6)) $suffix = numberToWordsID($number%pow(10,6));
$string = $prefix . " " . $suffix;
break;
case $number < pow(10,12):
$prefix = numberToWordsID(intval(floor($number/pow(10,9)))) . " milliar";
if ($number%pow(10,9)) $suffix = numberToWordsID($number%pow(10,9));
$string = $prefix . " " . $suffix;
break;
case $number < pow(10,15):
$prefix = numberToWordsID(intval(floor($number/pow(10,12)))) . " triliun";
if ($number%pow(10,12)) $suffix = numberToWordsID($number%pow(10,12));
$string = $prefix . " " . $suffix;
break;
case $number < pow(10,18):
$prefix = numberToWordsID(intval(floor($number/pow(10,15)))) . " kuadraliun";
if ($number%pow(10,15)) $suffix = numberToWordsID($number%pow(10,15));
$string = $prefix . " " . $suffix;
break;
}
}else{
return "ERROR - $number<br/> Number must be an integer between -" . number_format($max_size, 0, ".", ",") . " and " . number_format($max_size, 0, ".", ",");
}
return $string;
}
@fzn0x
Copy link
Author

fzn0x commented Dec 29, 2020

echo numberToWordsID(100000);
//EXPECTED OUTPUT : seratus ribu

@syauqi
Copy link

syauqi commented Dec 29, 2020

echo numberToWordsID(100000);
//EXPECTED OUTPUT : seratus ribu

Nice job!

@fzn0x
Copy link
Author

fzn0x commented Dec 29, 2020

Sorry for typo , fixed numberToWordsId -> numberToWordsID

@fzn0x
Copy link
Author

fzn0x commented Dec 29, 2020

echo numberToWordsID(100000);
//EXPECTED OUTPUT : seratus ribu

Nice job!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment