Skip to content
Permalink
Browse files

Use formatter method for ordinal numbers method

  • Loading branch information...
bcrowe committed Sep 4, 2015
1 parent 1493109 commit a8627b4014083a15fef8d17fed7b679e8d354f02
Showing with 13 additions and 5 deletions.
  1. +8 −5 src/I18n/Number.php
  2. +5 −0 tests/TestCase/I18n/NumberTest.php
@@ -253,7 +253,7 @@ public static function defaultCurrency($currency = null)
*
* - `locale` - The locale name to use for formatting the number, e.g. fr_FR
* - `type` - The formatter type to construct, set it to `currency` if you need to format
* numbers representing money.
* numbers representing money or 'ordinal' for the ordinal numbers.
* - `places` - Number of decimal places to use. e.g. 2
* - `precision` - Maximum Number of decimal places to use, e.g. 2
* - `pattern` - An ICU number pattern to use for formatting the number. e.g #,###.00
@@ -276,6 +276,10 @@ public static function formatter($options = [])
$type = NumberFormatter::CURRENCY;
}
if (!empty($options['type']) && $options['type'] === 'ordinal') {
$type = NumberFormatter::ORDINAL;
}
if (!isset(static::$_formatters[$locale][$type])) {
static::$_formatters[$locale][$type] = new NumberFormatter($locale, $type);
}
@@ -317,12 +321,11 @@ public static function formatter($options = [])
* Returns a formatted integer as an ordinal number string (e.g. 1st, 2nd, 3rd, 4th, [...])
*
* @param int|float $value An integer
* @param array $options An array with options.
* @return string
*/
public static function ordinal($value)
public static function ordinal($value, array $options = [])
{
$locale = I18n::locale();
$formatter = new NumberFormatter($locale, NumberFormatter::ORDINAL);
return $formatter->format($value);
return static::formatter(['type' => 'ordinal'] + $options)->format($value);
}
}
@@ -553,6 +553,11 @@ public function testOrdinal()
$result = $this->Number->ordinal(2);
$this->assertEquals('2nd', $result);
$result = $this->Number->ordinal(2, [
'locale' => 'fr_FR'
]);
$this->assertEquals('2e', $result);
$result = $this->Number->ordinal(3);
$this->assertEquals('3rd', $result);

0 comments on commit a8627b4

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.