16/06/2018 - PHP
Use this class to format money for given currency. I've got it from here.
declare(strict_types=1);
namespace App\Util;
use App\Exception\MoneyFormatterUtilException;
/**
* @link https://www.thefinancials.com/Default.aspx?SubSectionID=curformat
* @link https://gist.github.com/neo22s/9621415
*/
class MoneyFormatterUtil
{
private $currencies = [
'ARS' => [2,',','.'], // Argentine Peso
'AMD' => [2,'.',','], // Armenian Dram
'AWG' => [2,'.',','], // Aruban Guilder
'AUD' => [2,'.',' '], // Australian Dollar
'BSD' => [2,'.',','], // Bahamian Dollar
'BHD' => [3,'.',','], // Bahraini Dinar
'BDT' => [2,'.',','], // Bangladesh, Taka
'BZD' => [2,'.',','], // Belize Dollar
'BMD' => [2,'.',','], // Bermudian Dollar
'BOB' => [2,'.',','], // Bolivia, Boliviano
'BAM' => [2,'.',','], // Bosnia and Herzegovina, Convertible Marks
'BWP' => [2,'.',','], // Botswana, Pula
'BRL' => [2,',','.'], // Brazilian Real
'BND' => [2,'.',','], // Brunei Dollar
'CAD' => [2,'.',','], // Canadian Dollar
'KYD' => [2,'.',','], // Cayman Islands Dollar
'CLP' => [0,'','.'], // Chilean Peso
'CNY' => [2,'.',','], // China Yuan Renminbi
'COP' => [2,',','.'], // Colombian Peso
'CRC' => [2,',','.'], // Costa Rican Colon
'HRK' => [2,',','.'], // Croatian Kuna
'CUC' => [2,'.',','], // Cuban Convertible Peso
'CUP' => [2,'.',','], // Cuban Peso
'CYP' => [2,'.',','], // Cyprus Pound
'CZK' => [2,'.',','], // Czech Koruna
'DKK' => [2,',','.'], // Danish Krone
'DOP' => [2,'.',','], // Dominican Peso
'XCD' => [2,'.',','], // East Caribbean Dollar
'EGP' => [2,'.',','], // Egyptian Pound
'SVC' => [2,'.',','], // El Salvador Colon
'EUR' => [2,',','.'], // Euro
'GHC' => [2,'.',','], // Ghana, Cedi
'GIP' => [2,'.',','], // Gibraltar Pound
'GTQ' => [2,'.',','], // Guatemala, Quetzal
'HNL' => [2,'.',','], // Honduras, Lempira
'HKD' => [2,'.',','], // Hong Kong Dollar
'HUF' => [0,'','.'], // Hungary, Forint
'ISK' => [0,'','.'], // Iceland Krona
'INR' => [2,'.',','], // Indian Rupee
'IDR' => [2,',','.'], // Indonesia, Rupiah
'IRR' => [2,'.',','], // Iranian Rial
'JMD' => [2,'.',','], // Jamaican Dollar
'JPY' => [0,'',','], // Japan, Yen
'JOD' => [3,'.',','], // Jordanian Dinar
'KES' => [2,'.',','], // Kenyan Shilling
'KWD' => [3,'.',','], // Kuwaiti Dinar
'LVL' => [2,'.',','], // Latvian Lats
'LBP' => [0,'',' '], // Lebanese Pound
'LTL' => [2,',',' '], // Lithuanian Litas
'MKD' => [2,'.',','], // Macedonia, Denar
'MYR' => [2,'.',','], // Malaysian Ringgit
'MTL' => [2,'.',','], // Maltese Lira
'MUR' => [0,'',','], // Mauritius Rupee
'MXN' => [2,'.',','], // Mexican Peso
'MZM' => [2,',','.'], // Mozambique Metical
'NPR' => [2,'.',','], // Nepalese Rupee
'ANG' => [2,'.',','], // Netherlands Antillian Guilder
'ILS' => [2,'.',','], // New Israeli Shekel
'TRY' => [2,'.',','], // New Turkish Lira
'NZD' => [2,'.',','], // New Zealand Dollar
'NOK' => [2,',','.'], // Norwegian Krone
'PKR' => [2,'.',','], // Pakistan Rupee
'PEN' => [2,'.',','], // Peru, Nuevo Sol
'UYU' => [2,',','.'], // Peso Uruguayo
'PHP' => [2,'.',','], // Philippine Peso
'PLN' => [2,'.',' '], // Poland, Zloty
'GBP' => [2,'.',','], // Pound Sterling
'OMR' => [3,'.',','], // Rial Omani
'RON' => [2,',','.'], // Romania, New Leu
'ROL' => [2,',','.'], // Romania, Old Leu
'RUB' => [2,',','.'], // Russian Ruble
'SAR' => [2,'.',','], // Saudi Riyal
'SGD' => [2,'.',','], // Singapore Dollar
'SKK' => [2,',',' '], // Slovak Koruna
'SIT' => [2,',','.'], // Slovenia, Tolar
'ZAR' => [2,'.',' '], // South Africa, Rand
'KRW' => [0,'',','], // South Korea, Won
'SZL' => [2,'.',', '], // Swaziland, Lilangeni
'SEK' => [2,',','.'], // Swedish Krona
'CHF' => [2,'.','\''], // Swiss Franc
'TZS' => [2,'.',','], // Tanzanian Shilling
'THB' => [2,'.',','], // Thailand, Baht
'TOP' => [2,'.',','], // Tonga, Paanga
'AED' => [2,'.',','], // UAE Dirham
'UAH' => [2,',',' '], // Ukraine, Hryvnia
'USD' => [2,'.',','], // US Dollar
'VUV' => [0,'',','], // Vanuatu, Vatu
'VEF' => [2,',','.'], // Venezuela Bolivares Fuertes
'VEB' => [2,',','.'], // Venezuela, Bolivar
'VND' => [0,'','.'], // Viet Nam, Dong
'ZWD' => [2,'.',' '], // Zimbabwe Dollar
];
public function format(string $currency, int $money): string
{
$currency = strtoupper($currency);
if (!isset($this->currencies[$currency])) {
throw new RuntimeException(sprintf('The "%s" currency was not found.', $currency));
}
if ('INR' === $currency) {
$format = $this->formatINR((string) $money);
} else {
$format = number_format(
$money,
$this->currencies[$currency][0],
$this->currencies[$currency][1],
$this->currencies[$currency][2]
);
}
return (string) $format;
}
private function formatINR(string $money): string
{
$decimal = '';
$position = strpos($money, '.');
if ($position !== false) {
$decimal = substr(round(substr($money, $position), 2), 1);
$money = substr($money,0, $position);
}
$number = substr($money, -3);
$money = substr($money, 0, -3);
while (!is_bool($money) && strlen($money) > 0) {
$number = substr($money, -2).','.$number;
$money = substr($money, 0, -2);
}
return $number.$decimal;
}
}