Skip to content
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
7 changes: 6 additions & 1 deletion src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,19 @@ public static function percentage(int|float $number, int $precision = 0, ?int $m
* @param int|float $number
* @param string $in
* @param string|null $locale
* @param int|null $digits
* @return string|false
*/
public static function currency(int|float $number, string $in = 'USD', ?string $locale = null)
public static function currency(int|float $number, string $in = 'USD', ?string $locale = null, int $digits = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://en.wikipedia.org/wiki/Fraction

Feel like $fractions would be better than $digits

Copy link
Author

@emedchill emedchill Jan 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP has both fraction and digits in their naming convention (NumberFormatter::FRACTION_DIGITS) but I felt that simple number of $digits after the decimal was fine.

I can change it however we want though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe follow the other functions and use $precision?

{
static::ensureIntlExtensionIsInstalled();

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

if (! is_null($digits)) {
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $digits);
}

return $formatter->formatCurrency($number, $in);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public function testToCurrency()
$this->assertSame('-$5.00', Number::currency(-5));
$this->assertSame('$5.00', Number::currency(5.00));
$this->assertSame('$5.32', Number::currency(5.325));

$this->assertSame('$5', Number::currency(5.00, digits: 0));
$this->assertSame('$6', Number::currency(5.99, digits: 0));
}

public function testToCurrencyWithDifferentLocale()
Expand Down