Skip to content
Open
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
22 changes: 22 additions & 0 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,26 @@ protected static function ensureIntlExtensionIsInstalled()
throw new RuntimeException('The "intl" PHP extension is required to use the ['.$method.'] method.');
}
}

/**
* Format a number as a percentage with custom precision.
*
* @param float|int $number
* @param int $precision
* @param string|null $locale

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

⚠️ High: Delegate the formatPercentage method to the existing percentage method to ensure consistent behavior and reuse existing logic.

php
public static function formatPercentage($number, $precision = 2, $locale = null)
{
return static::percentage($number, $precision, null, $locale);
}

* @return string|false
*/
public static function formatPercentage($number, $precision = 2, $locale = null)
{

if (!is_numeric($number)) {
return false;
}

$formatter = new NumberFormatter($locale ?? app()->getLocale(), NumberFormatter::PERCENT);

$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);

return $formatter->format($number / 100);
}
}