Exemplo n.º 1
0
 public function testLocale()
 {
     $localeDetector = $this->getMock('Sonata\\IntlBundle\\Locale\\LocaleDetectorInterface');
     $localeDetector->expects($this->any())->method('getLocale')->will($this->returnValue('fr'));
     $helper = new NumberHelper('UTF-8', $localeDetector);
     // currency
     $this->assertEquals('10,49 €', $helper->formatCurrency(10.49, 'EUR'));
     $this->assertEquals('10,50 €', $helper->formatCurrency(10.499, 'EUR'));
     $this->assertEquals('10 000,50 €', $helper->formatCurrency(10000.499, 'EUR'));
     $this->assertEquals('10,49 €', $helper->formatCurrency(10.49, 'EUR', array('fraction_digits' => 0)));
     // decimal
     $this->assertEquals('10', $helper->formatDecimal(10));
     $this->assertEquals('10,155', $helper->formatDecimal(10.15459));
     $this->assertEquals('1 000 000,155', $helper->formatDecimal(1000000.15459));
     // scientific
     $this->assertEquals('1E1', $helper->formatScientific(10));
     $this->assertEquals('1E3', $helper->formatScientific(1000));
     $this->assertEquals('1,0001E3', $helper->formatScientific(1000.1));
     $this->assertEquals('1,00000015459E6', $helper->formatScientific(1000000.15459));
     $this->assertEquals('1,00000015459E6', $helper->formatScientific(1000000.15459));
     // duration
     $this->assertEquals('1 000 000', $helper->formatDuration(1000000));
     // spell out
     $this->assertEquals('un', $helper->formatSpellout(1));
     $this->assertEquals('quarante-deux', $helper->formatSpellout(42));
     if (version_compare(NumberHelper::getICUDataVersion(), '52', '>=')) {
         $this->assertEquals('un million deux cent vingt-quatre mille cinq cent cinquante-sept virgule un deux cinq quatre', $helper->formatSpellout(1224557.1254));
     } else {
         $this->assertEquals('un million deux-cent-vingt-quatre-mille-cinq-cent-cinquante-sept virgule un deux cinq quatre', $helper->formatSpellout(1224557.1254));
     }
     // percent
     $this->assertEquals('10 %', $helper->formatPercent(0.1));
     $this->assertEquals('200 %', $helper->formatPercent(1.999));
     $this->assertEquals('99 %', $helper->formatPercent(0.99));
     // ordinal
     if (version_compare(NumberHelper::getICUDataVersion(), '4.8.0', '>=')) {
         $this->assertEquals('1er', $helper->formatOrdinal(1), 'ICU Version: ' . NumberHelper::getICUDataVersion());
         $this->assertEquals('100e', $helper->formatOrdinal(100), 'ICU Version: ' . NumberHelper::getICUDataVersion());
         $this->assertEquals('10 000e', $helper->formatOrdinal(10000), 'ICU Version: ' . NumberHelper::getICUDataVersion());
     } elseif (version_compare(NumberHelper::getICUDataVersion(), '4.1.0', '>=')) {
         $this->assertEquals('1ᵉʳ', $helper->formatOrdinal(1), 'ICU Version: ' . NumberHelper::getICUDataVersion());
         $this->assertEquals('100ᵉ', $helper->formatOrdinal(100), 'ICU Version: ' . NumberHelper::getICUDataVersion());
         $this->assertEquals('10 000ᵉ', $helper->formatOrdinal(10000), 'ICU Version: ' . NumberHelper::getICUDataVersion());
     } else {
         $this->markTestIncomplete(sprintf('Unknown ICU DATA Version, feel free to contribute ... (version: %s)', NumberHelper::getICUDataVersion()));
     }
 }
 /**
  * @param float $number
  * @param array $attributes
  * @param array $textAttributes
  * @param null  $locale
  *
  * @return string
  */
 public function formatOrdinal($number, array $attributes = array(), array $textAttributes = array(), $locale = null)
 {
     return $this->helper->formatOrdinal($number, $attributes, $textAttributes, $locale);
 }