Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(Support): Add number parsing methods to Number class
Add three new methods to the Number class for parsing numeric strings:
- parse(): Parse string to number based on format type
- parseInt(): Parse string to integer with locale support
- parseFloat(): Parse string to float with locale support

These methods leverage the PHP Intl extension's NumberFormatter to provide
locale-aware number parsing capabilities.
  • Loading branch information
informagenie committed May 13, 2025
commit 5e9f6e1a4ac4aac836cb52a45d2faa26f1b0ee20
41 changes: 41 additions & 0 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,47 @@ public static function format(int|float $number, ?int $precision = null, ?int $m
return $formatter->format($number);
}

/**
* Parse the given string according to the specified format type.
*
* @param string $string
* @param int|null $type
* @param string|null $locale
* @return int|float|false
*/
public static function parse(string $string, ?int $type = NumberFormatter::TYPE_DOUBLE, ?string $locale = null): int|float
{
static::ensureIntlExtensionIsInstalled();

$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::DECIMAL);

return $formatter->parse($string, $type);
}

/**
* Parse a string into an integer according to the specified locale.
*
* @param string $string
* @param string|null $locale
* @return int|false
*/
public static function parseInt(string $string, ?string $locale = null): int
{
return self::parse($string, NumberFormatter::TYPE_INT32, $locale);
}

/**
* Parse a string into a float according to the specified locale.
*
* @param string $string The string to parse
* @param string|null $locale The locale to use
* @return float|false
*/
public static function parseFloat(string $string, ?string $locale = null ): float
{
return self::parse($string, NumberFormatter::TYPE_DOUBLE, $locale);
}

/**
* Spell out the given number in the given locale.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,39 @@ public function testTrim()
$this->assertSame(12.3456789, Number::trim(12.3456789));
$this->assertSame(12.3456789, Number::trim(12.34567890000));
}

#[RequiresPhpExtension('intl')]
public function testParse()
{
$this->assertSame(1234.0, Number::parse('1,234'));
$this->assertSame(1234.5, Number::parse('1,234.5'));
$this->assertSame(1234.56, Number::parse('1,234.56'));
$this->assertSame(-1234.56, Number::parse('-1,234.56'));

$this->assertSame(1234.56, Number::parse('1.234,56', locale: 'de'));
$this->assertSame(1234.56, Number::parse('1 234,56', locale: 'fr'));
}

#[RequiresPhpExtension('intl')]
public function testParseInt()
{
$this->assertSame(1234, Number::parseInt('1,234'));
$this->assertSame(1234, Number::parseInt('1,234.5'));
$this->assertSame(-1234, Number::parseInt('-1,234.56'));

$this->assertSame(1234, Number::parseInt('1.234', locale: 'de'));
$this->assertSame(1234, Number::parseInt('1 234', locale: 'fr'));
}

#[RequiresPhpExtension('intl')]
public function testParseFloat()
{
$this->assertSame(1234.0, Number::parseFloat('1,234'));
$this->assertSame(1234.5, Number::parseFloat('1,234.5'));
$this->assertSame(1234.56, Number::parseFloat('1,234.56'));
$this->assertSame(-1234.56, Number::parseFloat('-1,234.56'));

$this->assertSame(1234.56, Number::parseFloat('1.234,56', locale: 'de'));
$this->assertSame(1234.56, Number::parseFloat('1 234,56', locale: 'fr'));
}
}