Skip to content

Instantly share code, notes, and snippets.

@awolad
Created March 8, 2018 06:34
Show Gist options
  • Save awolad/3f6cc8568b18cd298fe20cee4e3f5697 to your computer and use it in GitHub Desktop.
Save awolad/3f6cc8568b18cd298fe20cee4e3f5697 to your computer and use it in GitHub Desktop.
<?php
//method one
function taka_format($amount = 0)
{
$tmp = explode('.', $amount); // for float or double values
$strMoney = '';
$divide = 1000;
$amount = $tmp[0];
$strMoney .= str_pad($amount % $divide, 3, '0', STR_PAD_LEFT);
$amount = (int)($amount / $divide);
while ($amount > 0) {
$divide = 100;
$strMoney = str_pad($amount % $divide, 2, '0', STR_PAD_LEFT) . ',' . $strMoney;
$amount = (int)($amount / $divide);
}
if (substr($strMoney, 0, 1) == '0')
$strMoney = substr($strMoney, 1);
if (isset($tmp[1])) // if float and double add the decimal digits here.
{
return $strMoney . '.' . $tmp[1];
}
return $strMoney;
}
//method two
function taka_format($amount = 0)
{
$tmp = explode('.', $amount); // for float or double values
$strMoney = '';
$amount = $tmp[0];
$strMoney .= substr($amount, -3, 3);
$amount = substr($amount, 0, -3);
while (strlen($amount) > 0) {
$strMoney = substr($amount, -2, 2) . ',' . $strMoney;
$amount = substr($amount, 0, -2);
}
if (isset($tmp[1])) // if float and double add the decimal digits here.
{
return $strMoney . '.' . $tmp[1];
}
return $strMoney;
}
//method three
function taka_format($amount = 0)
{
$tmp = explode('.', $amount); // for float or double values
$strMoney = '';
$amount = $tmp[0];
$strMoney .= substr($amount, -3, 3);
$amount = substr($amount, 0, -3);
while (strlen($amount) > 0) {
$strMoney = substr($amount, -2, 2) . ',' . $strMoney;
$amount = substr($amount, 0, -2);
}
if (isset($tmp[1])) // if float and double add the decimal digits here.
{
return $strMoney . '.' . $tmp[1][0] . $tmp[1][1]; // taken two numbers after decimal
}
return $strMoney . '.00';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment