diff options
-rw-r--r-- | includes/GlobalFunctions.php | 7 | ||||
-rw-r--r-- | tests/phpunit/includes/GlobalFunctions/GlobalTest.php | 1 |
2 files changed, 5 insertions, 3 deletions
diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index d08cf0e584e8..ca9ab582beb0 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1952,13 +1952,14 @@ function wfRecursiveRemoveDir( $dir ) { } /** - * @param float $nr The number to format + * @param float|int $nr The number to format * @param int $acc The number of digits after the decimal point, default 2 * @param bool $round Whether or not to round the value, default true * @return string */ -function wfPercent( $nr, $acc = 2, $round = true ) { - $ret = sprintf( "%.${acc}f", $nr ); +function wfPercent( $nr, int $acc = 2, bool $round = true ) { + $accForFormat = $acc >= 0 ? $acc : 0; + $ret = sprintf( "%.${accForFormat}f", $nr ); return $round ? round( (float)$ret, $acc ) . '%' : "$ret%"; } diff --git a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php index 36a9349bde04..e6461cd13ddc 100644 --- a/tests/phpunit/includes/GlobalFunctions/GlobalTest.php +++ b/tests/phpunit/includes/GlobalFunctions/GlobalTest.php @@ -401,6 +401,7 @@ class GlobalTest extends MediaWikiTestCase { [ 3 / 6, '0.5%' ], [ 1 / 3, '0%', 0 ], [ 10 / 3, '0%', -1 ], + [ 123.456, '120%', -1 ], [ 3 / 4 / 5, '0.1%', 1 ], [ 6 / 7 * 8, '6.8571428571%', 10 ], ]; |