*/ /** * patTemplate modfifier Number * * Formats dates and times according to a format string. * * Possible attributes are: * - decimals (int) * - point - use locale settings by default * - separator - use locale settings by default * - zeroreplace - replace tailing 0 * * See the PHP documentation for number_format() for * more information. */ class patTemplate_Modifier_Number extends patTemplate_Modifier { private $defaults = array( 'decimals' => 2, 'point' => '__LOCALE__', 'separator' => '__LOCALE__', 'zeroreplace' => '__NONE__' ); /** * Modify the value * * @param string value * @return string modified value */ public function modify($value, $params = array()) { // normalize $tmp = str_replace(',', '.', $value); if (!is_numeric($tmp)) { return $value; } $tmp = floatval($tmp); $params = array_merge($this->defaults, $params); $locale = localeconv(); if ('__LOCALE__' == strtoupper($params['point'])) { $params['point'] = $locale['decimal_point']; } if ('__LOCALE__' == strtoupper($params['separator'])) { $params['separator'] = $locale['thousands_sep']; } $suffix = ''; if ('__NONE__' != strtoupper($params['zeroreplace'])) { if (!strstr(strval($tmp), $params['point'])) { $params['decimals'] = 0; $suffix = $params['point'] . $params['zeroreplace']; } } $tmp = number_format($tmp, $params['decimals'], $params['point'], $params['separator']); return $tmp . $suffix; } }