Browse Source

Using Number::formatter to init NumberFormatter

CauanCabral 11 years ago
parent
commit
0252876739
2 changed files with 11 additions and 11 deletions
  1. 10 10
      src/I18n/Number.php
  2. 1 1
      tests/TestCase/I18n/NumberTest.php

+ 10 - 10
src/I18n/Number.php

@@ -14,7 +14,6 @@
  */
 namespace Cake\I18n;
 
-use Locale;
 use NumberFormatter;
 
 /**
@@ -130,20 +129,21 @@ class Number
     }
 
     /**
-     * Parse a localized numeric string and transform it
-     * in a float point
+     * Parse a localized numeric string and transform it in a float point
+     *
+     * Options:
+     *
+     * - `locale` - The locale name to use for parsing the number, e.g. fr_FR
+     * - `type` - The formatter type to construct, set it to `currency` if you need to parse
+     *    numbers representing money.
      *
      * @param string $value A numeric string.
-     * @param string|null $locale Locale of the formatted number
+     * @param array $options An array with options.
      * @return float point number
      */
-    public static function parseFloat($value, $locale = null)
+    public static function parseFloat($value, array $options = [])
     {
-        if ($locale === null) {
-            $locale = Locale::getDefault();
-        }
-
-        $formatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
+        $formatter = static::formatter($options);
         return (float)$formatter->parse($value, NumberFormatter::TYPE_DOUBLE);
     }
 

+ 1 - 1
tests/TestCase/I18n/NumberTest.php

@@ -114,7 +114,7 @@ class NumberTest extends TestCase
         $this->assertEquals($expected, $result);
 
         $value = '1,234.37';
-        $result = $this->Number->parseFloat($value, 'en_US');
+        $result = $this->Number->parseFloat($value, ['locale' => 'en_US']);
         $expected = 1234.37;
         $this->assertEquals($expected, $result);
     }