Created
November 19, 2013 21:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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