Skip to content

Commit 5c146fb

Browse files
authored
feat(Support): Add number parsing methods to Number class (#55725)
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.
1 parent a70c6f8 commit 5c146fb

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/Illuminate/Support/Number.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,47 @@ public static function format(int|float $number, ?int $precision = null, ?int $m
4848
return $formatter->format($number);
4949
}
5050

51+
/**
52+
* Parse the given string according to the specified format type.
53+
*
54+
* @param string $string
55+
* @param int|null $type
56+
* @param string|null $locale
57+
* @return int|float|false
58+
*/
59+
public static function parse(string $string, ?int $type = NumberFormatter::TYPE_DOUBLE, ?string $locale = null): int|float
60+
{
61+
static::ensureIntlExtensionIsInstalled();
62+
63+
$formatter = new NumberFormatter($locale ?? static::$locale, NumberFormatter::DECIMAL);
64+
65+
return $formatter->parse($string, $type);
66+
}
67+
68+
/**
69+
* Parse a string into an integer according to the specified locale.
70+
*
71+
* @param string $string
72+
* @param string|null $locale
73+
* @return int|false
74+
*/
75+
public static function parseInt(string $string, ?string $locale = null): int
76+
{
77+
return self::parse($string, NumberFormatter::TYPE_INT32, $locale);
78+
}
79+
80+
/**
81+
* Parse a string into a float according to the specified locale.
82+
*
83+
* @param string $string The string to parse
84+
* @param string|null $locale The locale to use
85+
* @return float|false
86+
*/
87+
public static function parseFloat(string $string, ?string $locale = null ): float
88+
{
89+
return self::parse($string, NumberFormatter::TYPE_DOUBLE, $locale);
90+
}
91+
5192
/**
5293
* Spell out the given number in the given locale.
5394
*

tests/Support/SupportNumberTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,39 @@ public function testTrim()
355355
$this->assertSame(12.3456789, Number::trim(12.3456789));
356356
$this->assertSame(12.3456789, Number::trim(12.34567890000));
357357
}
358+
359+
#[RequiresPhpExtension('intl')]
360+
public function testParse()
361+
{
362+
$this->assertSame(1234.0, Number::parse('1,234'));
363+
$this->assertSame(1234.5, Number::parse('1,234.5'));
364+
$this->assertSame(1234.56, Number::parse('1,234.56'));
365+
$this->assertSame(-1234.56, Number::parse('-1,234.56'));
366+
367+
$this->assertSame(1234.56, Number::parse('1.234,56', locale: 'de'));
368+
$this->assertSame(1234.56, Number::parse('1 234,56', locale: 'fr'));
369+
}
370+
371+
#[RequiresPhpExtension('intl')]
372+
public function testParseInt()
373+
{
374+
$this->assertSame(1234, Number::parseInt('1,234'));
375+
$this->assertSame(1234, Number::parseInt('1,234.5'));
376+
$this->assertSame(-1234, Number::parseInt('-1,234.56'));
377+
378+
$this->assertSame(1234, Number::parseInt('1.234', locale: 'de'));
379+
$this->assertSame(1234, Number::parseInt('1 234', locale: 'fr'));
380+
}
381+
382+
#[RequiresPhpExtension('intl')]
383+
public function testParseFloat()
384+
{
385+
$this->assertSame(1234.0, Number::parseFloat('1,234'));
386+
$this->assertSame(1234.5, Number::parseFloat('1,234.5'));
387+
$this->assertSame(1234.56, Number::parseFloat('1,234.56'));
388+
$this->assertSame(-1234.56, Number::parseFloat('-1,234.56'));
389+
390+
$this->assertSame(1234.56, Number::parseFloat('1.234,56', locale: 'de'));
391+
$this->assertSame(1234.56, Number::parseFloat('1 234,56', locale: 'fr'));
392+
}
358393
}

0 commit comments

Comments
 (0)