Skip to content

Instantly share code, notes, and snippets.

@imkingdavid
Created November 19, 2013 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imkingdavid/7552910 to your computer and use it in GitHub Desktop.
Save imkingdavid/7552910 to your computer and use it in GitHub Desktop.
Twig extension method to add a filter that formats currency similarly to the number_format filter, but with a few changes.
<?php
/**
* Basically a currency replacement for number_format
*
* Big differences are
*
* @param int $amount
* @param int $decimal Number of decimal places
* @param string $decimalPoint What to use as the decimal point
* @param string
* @param string $currency Symbol ($) of currency
*/
public function currency_format($amount, $decimal = 2, $decimalPoint = '.', $thousandSep = ',', $symbol = "$", $negativeParenthesis = true)
{
$working_amount = $amount;
if ($negativeParenthesis !== false) {
$working_amount = abs($working_amount);
}
$working_amount = number_format((float) $working_amount, $decimal, $decimalPoint, $thousandSep);
$return = "{$symbol}$working_amount";
return $negativeParenthesis !== false && $amount < 0 ? "($return)" : $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment