Skip to content

Commit 0edcfdd

Browse files
committed
Add number_format filter.
Refs twigphp#417
1 parent 670d5d7 commit 0edcfdd

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

lib/Twig/Extension/Core.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class Twig_Extension_Core extends Twig_Extension
1616
{
1717
protected $dateFormat = 'F j, Y H:i';
18+
protected $numberFormat = array(0, '.', ',');
1819

1920
/**
2021
* Sets the default format to be used by the date filter.
@@ -36,6 +37,28 @@ public function getDateFormat()
3637
return $this->dateFormat;
3738
}
3839

40+
/**
41+
* Sets the default format to be used by the number_format filter.
42+
*
43+
* @param integer $decimal The number of decimal places to use.
44+
* @param string $decimalPoint The character(s) to use for the decimal point.
45+
* @param string $thousandSep The character(s) to use for the thousands separator.
46+
*/
47+
public function setNumberFormat($decimal, $decimalPoint, $thousandSep)
48+
{
49+
$this->numberFormat = array($decimal, $decimalPoint, $thousandSep);
50+
}
51+
52+
/**
53+
* Get the default format used by the number_format filter.
54+
*
55+
* @return array The arguments for number_format()
56+
*/
57+
public function getNumberFormat()
58+
{
59+
return $this->numberFormat;
60+
}
61+
3962
/**
4063
* Returns the token parser instance to add to the existing list.
4164
*
@@ -73,6 +96,7 @@ public function getFilters()
7396
'date' => new Twig_Filter_Function('twig_date_format_filter', array('needs_environment' => true)),
7497
'format' => new Twig_Filter_Function('sprintf'),
7598
'replace' => new Twig_Filter_Function('strtr'),
99+
'number_format' => new Twig_Filter_Function('twig_number_format_filter', array('needs_environment' => true)),
76100

77101
// encoding
78102
'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'),
@@ -310,6 +334,36 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $
310334
return $date->format($format);
311335
}
312336

337+
/**
338+
* Number format filter.
339+
*
340+
* All of the formatting options can be left null, in that case the defaults will
341+
* be used. Supplying any of the parameters will override the defaults set in the
342+
* environment object.
343+
*
344+
* @param Twig_Environment $env A Twig_Environment instance
345+
* @param mixed $number A float/int/string of the number to format
346+
* @param int $decimal The number of decimal points to display.
347+
* @param string $decimalPoint The character(s) to use for the decimal point.
348+
* @param string $thousandSep The character(s) to use for the thousands separator.
349+
*
350+
* @return string The formatted number
351+
*/
352+
function twig_number_format_filter(Twig_Environment $env, $number, $decimal = null, $decimalPoint = null, $thousandSep = null)
353+
{
354+
$defaults = $env->getExtension('core')->getNumberFormat();
355+
if ($decimal === null) {
356+
$decimal = $defaults[0];
357+
}
358+
if ($decimalPoint === null) {
359+
$decimalPoint = $defaults[1];
360+
}
361+
if ($thousandSep === null) {
362+
$thousandSep = $defaults[2];
363+
}
364+
return number_format((float) $number, $decimal, $decimalPoint, $thousandSep);
365+
}
366+
313367
/**
314368
* URL encodes a string.
315369
*
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
"number_format" filter
3+
--TEMPLATE--
4+
{{ 20|number_format }}
5+
{{ 20.25|number_format }}
6+
{{ 20.25|number_format(2) }}
7+
{{ 20.25|number_format(2, ',') }}
8+
{{ 1020.25|number_format(2, ',') }}
9+
{{ 1020.25|number_format(2, ',', '.') }}
10+
--DATA--
11+
return array();
12+
--EXPECT--
13+
20
14+
20
15+
20.25
16+
20,25
17+
1,020,25
18+
1.020,25
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
"number_format" filter with defaults.
3+
--TEMPLATE--
4+
{{ 20|number_format }}
5+
{{ 20.25|number_format }}
6+
{{ 20.25|number_format(1) }}
7+
{{ 20.25|number_format(2, ',') }}
8+
{{ 1020.25|number_format }}
9+
{{ 1020.25|number_format(2, ',') }}
10+
{{ 1020.25|number_format(2, ',', '.') }}
11+
--DATA--
12+
$twig->getExtension('core')->setNumberFormat(2, '!', '=');
13+
return array();
14+
--EXPECT--
15+
20!00
16+
20!25
17+
20!3
18+
20,25
19+
1=020!25
20+
1=020,25
21+
1.020,25

0 commit comments

Comments
 (0)