Skip to content

Commit

Permalink
Added nicenumber twig filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Jul 19, 2017
1 parent 039d228 commit 6fadaa0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Added `Grav\Framework\Object` classes for creating collections of objects
* Added `Grav\Framework\Page` interfaces
* Deprecated GravTrait
* Added `|nicenumber` Twig filter
1. [](#improved)
* Make it possible to include debug bar also into non-HTML responses

Expand Down
25 changes: 25 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function getFilters()
new \Twig_SimpleFilter('*ize', [$this, 'inflectorFilter']),
new \Twig_SimpleFilter('absolute_url', [$this, 'absoluteUrlFilter']),
new \Twig_SimpleFilter('contains', [$this, 'containsFilter']),
new \Twig_SimpleFilter('nicenumber', [$this, 'niceNumberFunc']),
new \Twig_SimpleFilter('defined', [$this, 'definedDefaultFilter']),
new \Twig_SimpleFilter('ends_with', [$this, 'endsWithFilter']),
new \Twig_SimpleFilter('fieldName', [$this, 'fieldNameFilter']),
Expand Down Expand Up @@ -1048,4 +1049,28 @@ public function pathinfoFunc($var)
{
return pathinfo($var);
}

/**
* Returns a nicer more readable number
*
* @param $number
* @return bool|string
*/
public function niceNumberFunc($n)
{

// first strip any formatting;
$n = (0+str_replace(",", "", $n));

// is this a number?
if (!is_numeric($n)) return false;

// now filter it;
if ($n > 1000000000000) return round(($n/1000000000000), 2).' t';
elseif ($n > 1000000000) return round(($n/1000000000), 2).' b';
elseif ($n > 1000000) return round(($n/1000000), 2).' m';
elseif ($n > 1000) return round(($n/1000), 2).' k';

return number_format($n);
}
}

0 comments on commit 6fadaa0

Please sign in to comment.