Skip to content

Commit

Permalink
- Style fixes;
Browse files Browse the repository at this point in the history
  • Loading branch information
andrius-386 committed Nov 3, 2023
1 parent fcc989c commit 1aa9eed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
30 changes: 17 additions & 13 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@

namespace Illuminate\Support;

class Number {
class Number
{
protected static array $formatsByLocale = [
'en' => [
'precision' => 2,
'format' => '%s%s',
'thousands_separator' => ',',
'decimal_separator' => '.'
'decimal_separator' => '.',
],
'fr' => [
'precision' => 2,
'format' => '%s %s',
'thousands_separator' => ' ',
'decimal_separator' => ','
'decimal_separator' => ',',
],
'de' => [
'precision' => 2,
'format' => '%s %s',
'thousands_separator' => '.',
'decimal_separator' => ','
'decimal_separator' => ',',
]
];

/**
* Percentage format with locale support.
*
* @param int|float $value
* @param array $options
* @param int|float $value
* @param array $options
* @return string
*/
public static function percentage(int|float $value, array $options = []): string {
public static function percentage(int|float $value, array $options = []): string
{
$defaults = [
'strip_insignificant_zeros' => false,
];
Expand All @@ -43,17 +45,18 @@ public static function percentage(int|float $value, array $options = []): string
$formatted = rtrim($formatted, '0\.');
}

return sprintf($options['format'],$formatted, '%');
return sprintf($options['format'], $formatted, '%');
}

/**
* Format the given number.
*
* @param int|float $value
* @param array $options
* @param int|float $value
* @param array $options
* @return string
*/
public static function format(int|float $value, array $options = []): string {
public static function format(int|float $value, array $options = []): string
{
$locale = $options['locale'] ?? 'en';

$options = array_merge(self::getFormatByLocale($locale), $options);
Expand All @@ -63,10 +66,11 @@ public static function format(int|float $value, array $options = []): string {
/**
* Return the format options for the given locale.
*
* @param string $locale
* @param string $locale
* @return array
*/
protected static function getFormatByLocale(string $locale = 'en'): array {
protected static function getFormatByLocale(string $locale = 'en'): array
{
if (isset(self::$formatsByLocale[$locale])) {
return self::$formatsByLocale[$locale];
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

class SupportNumberTest extends TestCase
{
public function testPercentage() {
public function testPercentage()
{
$this->assertSame('23.50%', Number::percentage(23.5));
$this->assertSame('23.50 %', Number::percentage(23.5, ['format' => '%s %s']));
$this->assertSame('23.501%', Number::percentage(23.501, ['precision' => 3]));
Expand All @@ -23,7 +24,7 @@ public function testPercentage() {
$this->assertSame('306%', Number::percentage(306.00, ['strip_insignificant_zeros' => true]));
$this->assertSame('307.11%', Number::percentage(307.110, ['precision' => 3, 'strip_insignificant_zeros' => true]));

$this->expectException(\RuntimeException::class, );
$this->expectException(\RuntimeException::class);
Number::percentage(23.5, ['locale' => 'es']);
}
}

0 comments on commit 1aa9eed

Please sign in to comment.