public function testShortNotation() { $this->assertEquals(0.12345, Format::getNumber(0.12345)); $options = array('locale' => 'de'); $this->assertEquals(0.12345, Format::getNumber(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0.12345, Format::getNumber(',12345', $options)); $this->assertEquals('0,75', Format::toNumber(0.75, array('locale' => 'de_DE', 'precision' => 2))); $this->assertTrue(Format::isNumber(',12345', array('locale' => 'de_AT'))); $this->assertEquals(0.12345, Format::getFloat(0.12345)); $options = array('locale' => 'de'); $this->assertEquals(0.12345, Format::getFloat(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0.12345, Format::getFloat(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals('0,12345', Format::toFloat(0.12345, $options)); $options = array('locale' => 'ar_QA'); $this->assertEquals('0,12345', Format::toFloat(0.12345, $options)); $this->assertTrue(Format::isFloat(',12345', array('locale' => 'de_AT'))); $this->assertEquals(0, Format::getInteger(0.1234567)); $options = array('locale' => 'de'); $this->assertEquals(0, Format::getInteger(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0, Format::getInteger(',12345', $options)); $this->assertEquals('0', Format::toInteger(0.123, array('locale' => 'de'))); $options = array('locale' => 'de_AT'); $this->assertEquals('0', Format::toInteger(0.12345, $options)); $this->assertFalse(Format::isInteger(',12345', array('locale' => 'de_AT'))); $options = array('locale' => 'de_AT'); $this->assertEquals('0,567', Format::toNumber(0.5669999999999999, $options)); }
/** * Returns true if and only if $value is a floating-point value * * @param string $value * @return boolean */ public function isValid($value) { if (!is_string($value) && !is_int($value) && !is_float($value)) { $this->_error(self::INVALID); return false; } if (is_float($value)) { return true; } $this->_setValue($value); try { if (!Locale\Format::isFloat($value, array('locale' => $this->_locale))) { $this->_error(self::NOT_FLOAT); return false; } } catch (Locale\Exception $e) { $this->_error(self::NOT_FLOAT); return false; } return true; }
/** * Defined by Zend_Validate_Interface * * Returns true if and only if $value is a floating-point value * * @param string $value * @return boolean */ public function isValid($value) { if (!is_string($value) && !is_int($value) && !is_float($value)) { $this->_error(self::INVALID); return false; } if (is_float($value)) { return true; } $this->_setValue($value); if ($this->_locale === null) { $locale = localeconv(); $valueFiltered = str_replace($locale['thousands_sep'], '', (string) $value); $valueFiltered = str_replace($locale['decimal_point'], '.', $valueFiltered); if (strval(floatval($valueFiltered)) != $valueFiltered) { $this->_error(self::NOT_FLOAT); return false; } } else { try { if (!Locale\Format::isFloat($value, array('locale' => 'en')) && !Locale\Format::isFloat($value, array('locale' => $this->_locale))) { $this->_error(self::NOT_FLOAT); return false; } } catch (Locale\Exception $e) { $this->_error(self::NOT_FLOAT); return false; } } return true; }