/** * Defined by Zend_Filter_Interface * * Normalizes the given input * * @param string $value Value to normalized * @return string|array The normalized value */ public function filter($value) { if (is_array($value)) { require_once 'Zend/Date.php'; $date = new Zend_Date($value, $this->_options['locale']); return $date->toString($this->_options['date_format']); } else { if ($this->_options['precision'] === 0) { return Zend_Locale_Format::toInteger($value, $this->_options); } else { if ($this->_options['precision'] === null) { return Zend_Locale_Format::toFloat($value, $this->_options); } } } return Zend_Locale_Format::toNumber($value, $this->_options); }
/** * Format in drive units * * @param int $size * @return string */ public static function getByteSized($size) { $units = array('', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'); $power = $size > 0 ? floor(log($size, 1024)) : 0; return \Zend_Locale_Format::toInteger($size / pow(1024, $power)) . ' ' . $units[$power]; }
/** * переводит значение заданного типа в строку * * @param mixed $data значение, которое нужно сконвертировать * @param string $type тип данных ('bool','date') * @param int $precision_default задание точного количества знаков после запятой * @return string полученная строка, представляющая данные */ function type_to_str($data, $type, $precision_default = -1) { switch ($type) { case 'encode': return htmlentities($data, ENT_QUOTES, 'utf-8'); case 'translate': return __($data); case "integer": /** * Локально пустые значения спокойно форматируется зендом в 0, * но на сервере, на orbitscipts.com? после форматирования пустого значение, * оно так и остаётся пустое. Я не знаю почему. */ if (is_null($data) || $data == '') { $data = 0; } $res = Zend_Locale_Format::toInteger($data, array('locale' => get_instance()->locale)); return $res; case "float": $precision = 2; /** * Определние нужной точности для денег */ if ($precision_default < 0) { $vals = explode('.', strval((double) $data)); if (isset($vals[1])) { $len = strlen($vals[1]); if ($len > 3) { $precision = 4; } elseif ($len > 2) { $precision = 3; } } } else { $precision = $precision_default; } return number_format($data, $precision, '.', ''); //return Zend_Locale_Format::toFloat($data, array('locale'=> get_instance()->locale)); //return Zend_Locale_Format::toFloat($data, array('locale'=> get_instance()->locale)); case 'procent': return type_to_str($data, 'float', 2) . ' %'; case "money": //return str_replace('%', type_to_str($data, 'float'), get_format('money')); if (is_null($data)) { $data = 0; } /** * @todo Возможно прийдётся определять тип валюты как-то глобально, * когда понадобится использовать что-нибудь отличное от доллара. */ try { $currency = new Zend_Currency(get_instance()->locale, 'USD'); } catch (Zend_Currency_Exception $e) { $currency = new Zend_Currency(get_instance()->default_locale, 'USD'); } $precision = 2; /** * Определние нужной точности для денег */ $vals = explode('.', strval((double) $data)); if (isset($vals[1])) { $len = strlen($vals[1]); } $currency->setFormat(array('precision' => $precision)); try { $value = trim($currency->toCurrency($data)); /** * проверка preg_matchем нужна потмоу что, например в китайском валюта отображается * как: US$0.00 */ if (get_instance()->locale != 'en_US' && preg_match('|^[0-9,\\.\\$\\s\\xc2\\xa0]+$|', $value, $matches)) { $value = '$' . trim(str_replace('$', '', $value)); } } catch (Exception $e) { $value = '0.00'; } return $value; case "nonzeromoney": if ($data == 0) { return "—"; } return type_to_str($data, "money"); case "mysqldate": $data = mktime(0, 0, 0, substr($data, 5, 2), substr($data, 8, 2), substr($data, 0, 4)); return date(get_date_format(), $data); case "mysqlfloat": //return str_replace(',','.',$data); return number_format($data, 8, '.', ''); case 'databasedate': return date("Y-m-d", $data); case 'databasedatetime': return date("Y-m-d H:i:s", $data); case "date": if ($data == 0 || is_null($data)) { return ''; } return date(get_date_format(), $data); case "datetime": $res = date(get_date_format(), $data) . ' ' . date(get_time_format(), $data); return $res; case "bool": return $data ? "true" : "false"; case 'textcode': $num = (int) ($data ^ 67894523); $text = ''; while ($num) { $text .= chr(ord('a') + $num % 26); $num = (int) ($num / 26); } return $text; case "impressions": if ($data < 1000) { return __('< 1 K'); } if ($data < 5000) { return __('1~5 K'); } if ($data < 10000) { return __('5~10 K'); } if ($data < 50000) { return __('10~50 K'); } if ($data < 100000) { return __('50~100 K'); } else { return __('> 100 K'); } case "clicks": if ($data < 100) { return __('< 100'); } if ($data < 900) { return '~ ' . round($data / 100) * 100; } else { return '~ ' . round($data / 1000) . ' K'; } case "mime": return '=?UTF-8?B?' . base64_encode($data) . '?='; default: return $data; } }
/** * test toInteger * expected string */ public function testtoInteger() { $this->assertEquals('0', Zend_Locale_Format::toInteger(0 )); $this->assertEquals('0', Zend_Locale_Format::toInteger(0, array('locale' => 'de'))); $options = array('locale' => 'de_AT'); $this->assertEquals( '0', Zend_Locale_Format::toInteger( 0, $options)); $this->assertEquals( '−1.234.567', Zend_Locale_Format::toInteger(-1234567, $options)); $this->assertEquals( '1.234.567', Zend_Locale_Format::toInteger( 1234567, $options)); $this->assertEquals( '0', Zend_Locale_Format::toInteger( 0.1234567, $options)); $this->assertEquals( '−1.234.567', Zend_Locale_Format::toInteger(-1234567.12345, $options)); $this->assertEquals( '1.234.567', Zend_Locale_Format::toInteger( 1234567.12345, $options)); $this->assertEquals( '1234567', Zend_Locale_Format::toInteger( 1234567.12345, array('locale' => 'ar_QA'))); $this->assertEquals( '1234567-', Zend_Locale_Format::toInteger(-1234567.12345, array('locale' => 'ar_QA'))); $this->assertEquals( '12,34,567', Zend_Locale_Format::toInteger( 1234567.12345, array('locale' => 'dz_BT'))); $this->assertEquals('-(1.234.567)', Zend_Locale_Format::toInteger(-1234567.12345, array('locale' => 'mk_MK'))); }
public function testShortNotation() { $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(0.12345)); $options = array('locale' => 'de'); $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(',12345', $options)); $this->assertEquals('0,75', Zend_Locale_Format::toNumber(0.75, array('locale' => 'de_DE', 'precision' => 2))); $this->assertTrue(Zend_Locale_Format::isNumber(',12345', array('locale' => 'de_AT'))); $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(0.12345)); $options = array('locale' => 'de'); $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals('0,12345', Zend_Locale_Format::toFloat(0.12345, $options)); $options = array('locale' => 'ar_QA'); $this->assertEquals('0,12345', Zend_Locale_Format::toFloat(0.12345, $options)); $this->assertTrue(Zend_Locale_Format::isFloat(',12345', array('locale' => 'de_AT'))); $this->assertEquals(0, Zend_Locale_Format::getInteger(0.1234567)); $options = array('locale' => 'de'); $this->assertEquals(0, Zend_Locale_Format::getInteger(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0, Zend_Locale_Format::getInteger(',12345', $options)); $this->assertEquals('0', Zend_Locale_Format::toInteger(0.123, array('locale' => 'de'))); $options = array('locale' => 'de_AT'); $this->assertEquals('0', Zend_Locale_Format::toInteger(0.12345, $options)); $this->assertFalse(Zend_Locale_Format::isInteger(',12345', array('locale' => 'de_AT'))); $options = array('locale' => 'de_AT'); $this->assertEquals('0,567', Zend_Locale_Format::toNumber(0.5669999999999999, $options)); }
/** * test toInteger * expected string */ public function testtoInteger() { $this->assertEquals(Zend_Locale_Format::toInteger(0), '0', "string 0 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(0, array('locale' => 'de')), '0', "string 0 expected"); $options = array('locale' => 'de_AT'); $this->assertEquals(Zend_Locale_Format::toInteger(0, $options), '0', "string 0 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(-1234567, $options), '-1.234.567', "string -1.234.567 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(1234567, $options), '1.234.567', "string 1.234.567 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(0.1234567, $options), '0', "string 0 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(-1234567.12345, $options), '-1.234.567', "string -1.234.567 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(1234567.12345, $options), '1.234.567', "value 1.234.567 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(1234567.12345, array('locale' => 'ar_QA')), '1234567', "value 1234567 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(-1234567.12345, array('locale' => 'ar_QA')), '1234567-', "value 1234567- expected"); $this->assertEquals(Zend_Locale_Format::toInteger(1234567.12345, array('locale' => 'dz_BT')), '12,34,567', "value 12,34,567 expected"); $this->assertEquals(Zend_Locale_Format::toInteger(-1234567.12345, array('locale' => 'mk_MK')), '-(1.234.567)', "value -(1.234.567) expected"); }
/** * test with two seperation language locale * expected string */ public function testIntegerRegionWithTwoSeperatedNegative() { $value = Zend_Locale_Format::toInteger(-1234567.12345, 'mk_MK'); $this->assertEquals($value, '-(1.234.567)', "value -(1.234.567) expected"); }
function numberFormat($number, $integer = false) { if ($integer) { return Zend_Locale_Format::toInteger($number, array('locale' => $GLOBALS['registry']->currentLang)); } return Zend_Locale_Format::toNumber($number, array('precision' => NUMBER_FORMAT_PRECISION, 'locale' => $GLOBALS['registry']->currentLang)); }