示例#1
0
 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));
 }
示例#2
0
 /**
  * Set a new value
  *
  * @param  integer                   $value  Value
  * @param  string                    $type   (Optional) A Zend\Measure\Number Type
  * @param  string|Zend\Locale\Locale $locale (Optional) A Zend\Locale\Locale Type
  * @throws Zend\Measure\Exception
  */
 public function setValue($value, $type = null, $locale = null)
 {
     if (empty($locale)) {
         $locale = $this->_locale;
     }
     if (empty($this->_units[$type])) {
         throw new Exception('unknown type of number:' . $type);
     }
     switch ($type) {
         case 'BINARY':
             preg_match('/[01]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'TERNARY':
             preg_match('/[012]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'QUATERNARY':
             preg_match('/[0123]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'QUINARY':
             preg_match('/[01234]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'SENARY':
             preg_match('/[012345]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'SEPTENARY':
             preg_match('/[0123456]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'OCTAL':
             preg_match('/[01234567]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'NONARY':
             preg_match('/[012345678]+/', $value, $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'DUODECIMAL':
             preg_match('/[0123456789AB]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'HEXADECIMAL':
             preg_match('/[0123456789ABCDEF]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         case 'ROMAN':
             preg_match('/[IVXLCDM_]+/', strtoupper($value), $ergebnis);
             $value = $ergebnis[0];
             break;
         default:
             try {
                 $value = Locale\Format::getInteger($value, array('locale' => $locale));
             } catch (\Exception $e) {
                 throw new Exception($e->getMessage(), $e->getCode(), $e);
             }
             if (call_user_func(Math::$comp, $value, 0) < 0) {
                 $value = call_user_func(Math::$sqrt, call_user_func(Math::$pow, $value, 2));
             }
             break;
     }
     $this->_value = $value;
     $this->_type = $type;
 }