Skip to content

Instantly share code, notes, and snippets.

@nulltier
Last active August 29, 2015 14:15
Show Gist options
  • Save nulltier/e04a80ec65141ae7159e to your computer and use it in GitHub Desktop.
Save nulltier/e04a80ec65141ae7159e to your computer and use it in GitHub Desktop.
<?php
/**
* Formats decimal numbers with the PHP's number_format().
* Use it as output filter like: [[+decimalNumber:NumberFormat=`d=2&ds=,&ts=.`]]
*
* Parameters:
* d - Decimals. The number of decimals to show. Default to 2
* ds - Decimal seperator. Default to '.'
* ts - Thousands separator. Default empty
*
* @author Bert Oost at OostDesign.nl <[email protected]>
* https://gist.github.com/bertoost/3898261
*/
$opts = array();
$options = ((!empty($options)) ? explode('&', $options) : '');
if(!empty($options) && is_array($options)) {
foreach($options as $option) {
list($key, $value) = explode('=', $option);
$opts[$key] = $value;
}
}
$input = ((!empty($input)) ? (float) str_replace(',', '.', $input) : $input);
$decimals = $modx->getOption('d', $opts, 0);
$seperator = $modx->getOption('ds', $opts, ',');
$thousands = $modx->getOption('ts', $opts, '');
if(!empty($input) && is_float($input)) {
// is here fractional part
if (fmod($input, 1) == 0){
$decimals = 0;
}
$output = number_format($input, $decimals, $seperator, $thousands);
return $output;
}
return $input;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment