예제 #1
0
    /**
     * Returns the internal value
     *
     * @param integer                   $round  (Optional) Rounds the value to an given precision,
     *                                                     Default is -1 which returns without rounding
     * @param string|Zend\Locale\Locale $locale (Optional) Locale for number representation
     * @return integer|string
     */
    public function getValue($round = -1, $locale = null)
    {
        if ($round < 0) {
            $return = $this->_value;
        } else {
            $return = Math::round($this->_value, $round);
        }

        if ($locale !== null) {
            $this->setLocale($locale, true);
            return Locale\Format::toNumber($return, array('locale' => $locale));
        }

        return $return;
    }
예제 #2
0
 /**
  * @group ZF-9319
  */
 public function testToNumberWithoutFormatWithPrecision()
 {
     $options = array('locale' => 'de_AT', 'precision' => 2);
     $this->assertEquals('3,99', \Zend\Locale\Format::toNumber(3.99, $options));
     $this->assertEquals('3,99', \Zend\Locale\Format::toNumber(3.994, $options));
     $this->assertEquals('4,00', \Zend\Locale\Format::toNumber(3.995, $options));
     $this->assertEquals('4,00', \Zend\Locale\Format::toNumber(3.999, $options));
     $this->assertEquals('4,00', \Zend\Locale\Format::toNumber(4, $options));
 }
예제 #3
0
파일: Currency.php 프로젝트: nevvermind/zf2
 /**
  * Returns a localized currency string
  *
  * @param  integer|float $value   OPTIONAL Currency value
  * @param  array         $options OPTIONAL options to set temporary
  * @throws Zend\Currency\Exception When the value is not a number
  * @return string
  */
 public function toCurrency($value = null, array $options = array())
 {
     if ($value === null) {
         if (is_array($options) && isset($options['value'])) {
             $value = $options['value'];
         } else {
             $value = $this->_options['value'];
         }
     }
     if (is_array($value)) {
         $options += $value;
         if (isset($options['value'])) {
             $value = $options['value'];
         }
     }
     // Validate the passed number
     if (!isset($value) or is_numeric($value) === false) {
         throw new Exception\InvalidArgumentException("Value '{$value}' has to be numeric");
     }
     if (isset($options['currency'])) {
         if (!isset($options['locale'])) {
             $options['locale'] = $this->_options['locale'];
         }
         $options['currency'] = self::getShortName($options['currency'], $options['locale']);
         $options['name'] = self::getName($options['currency'], $options['locale']);
         $options['symbol'] = self::getSymbol($options['currency'], $options['locale']);
     }
     $options = $this->_checkOptions($options) + $this->_options;
     // Format the number
     $format = $options['format'];
     $locale = $options['locale'];
     if (empty($format)) {
         $format = Cldr::getContent($locale, 'currencynumber');
     } else {
         if (Locale\Locale::isLocale($format, true)) {
             $locale = $format;
             $format = Cldr::getContent($format, 'currencynumber');
         }
     }
     $original = $value;
     $value = Locale\Format::toNumber($value, array('locale' => $locale, 'number_format' => $format, 'precision' => $options['precision']));
     if ($options['position'] !== self::STANDARD) {
         $value = str_replace('¤', '', $value);
         $space = '';
         if (iconv_strpos($value, ' ') !== false) {
             $value = str_replace(' ', '', $value);
             $space = ' ';
         }
         if ($options['position'] == self::LEFT) {
             $value = '¤' . $space . $value;
         } else {
             $value = $value . $space . '¤';
         }
     }
     // Localize the number digits
     if (empty($options['script']) === false) {
         $value = Locale\Format::convertNumerals($value, 'Latn', $options['script']);
     }
     // Get the sign to be placed next to the number
     if (is_numeric($options['display']) === false) {
         $sign = $options['display'];
     } else {
         switch ($options['display']) {
             case self::USE_SYMBOL:
                 $sign = $this->_extractPattern($options['symbol'], $original);
                 break;
             case self::USE_SHORTNAME:
                 $sign = $options['currency'];
                 break;
             case self::USE_NAME:
                 $sign = $options['name'];
                 break;
             default:
                 $sign = '';
                 $value = str_replace(' ', '', $value);
                 break;
         }
     }
     $value = str_replace('¤', $sign, $value);
     return $value;
 }
    /**
     * 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)) {
            $date = new Date($value, $this->_options['locale']);
            return $date->toString($this->_options['date_format']);
        } else if ($this->_options['precision'] === 0) {
            return Format::toInteger($value, $this->_options);
        } else if ($this->_options['precision'] === null) {
            return Format::toFloat($value, $this->_options);
        }

        return Format::toNumber($value, $this->_options);
    }