Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add Number class; convert given number to percentage. #48909

Closed
wants to merge 2 commits into from
Closed
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
80 changes: 80 additions & 0 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Illuminate\Support;

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

/**
* Percentage format with locale support.
*
* @param int|float $value
* @param array $options
* @return string
*/
public static function percentage(int|float $value, array $options = []): string
{
$defaults = [
'strip_insignificant_zeros' => false,
];
$locale = $options['locale'] ?? 'en';
$options = array_merge($defaults, self::getFormatByLocale($locale), $options);

$formatted = self::format($value, $options);
if ($options['strip_insignificant_zeros']) {
$formatted = rtrim($formatted, '0\.');
}

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

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

$options = array_merge(self::getFormatByLocale($locale), $options);
return number_format($value, $options['precision'], $options['decimal_separator'], $options['thousands_separator']);
}

/**
* Return the format options for the given locale.
*
* @param string $locale
* @return array
*/
protected static function getFormatByLocale(string $locale = 'en'): array
{
if (isset(self::$formatsByLocale[$locale])) {
return self::$formatsByLocale[$locale];
}

throw new \RuntimeException("Unsupported locale '$locale'");
}
}
30 changes: 30 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Illuminate\Tests\Support;

use Illuminate\Support\Number;
use PHPUnit\Framework\TestCase;

class SupportNumberTest extends TestCase
{
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]));
$this->assertSame('1 305,01 %', Number::percentage(1305.01, ['locale' => 'fr']));
$this->assertSame('9 988 776,65 %', Number::percentage(9988776.65, ['locale' => 'fr']));
$this->assertSame('9.988.776,65 %', Number::percentage(9988776.65, ['locale' => 'de']));
$this->assertSame('9,988,776.65%', Number::percentage(9988776.65, ['locale' => 'en']));
$this->assertSame('135,00 %', Number::percentage(135.0, ['locale' => 'fr']));
$this->assertSame('1,305.04%', Number::percentage(1305.04, ['locale' => 'en']));
$this->assertSame('1,305%', Number::percentage(1305.034, ['precision' => 0]));
$this->assertSame('1,305.4%', Number::percentage(1305.400, ['strip_insignificant_zeros' => true]));
$this->assertSame('305%', Number::percentage(305, ['strip_insignificant_zeros' => true]));
$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);
Number::percentage(23.5, ['locale' => 'es']);
}
}