diff --git a/lib/moodlelib.php b/lib/moodlelib.php index d10f31d7a97..1b69c4a1281 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8630,9 +8630,10 @@ function generate_password($maxlen=10) { * then it will display '5.4' instead of '5.400' or '5' instead of '5.000'. * * @param float $float The float to print - * @param int $decimalpoints The number of decimal places to print. + * @param int $decimalpoints The number of decimal places to print. -1 is a special value for auto detect (full precision). * @param bool $localized use localized decimal separator - * @param bool $stripzeros If true, removes final zeros after decimal point + * @param bool $stripzeros If true, removes final zeros after decimal point. It will be ignored and the trailing zeros after + * the decimal point are always striped if $decimalpoints is -1. * @return string locale float */ function format_float($float, $decimalpoints=1, $localized=true, $stripzeros=false) { @@ -8644,6 +8645,13 @@ function format_float($float, $decimalpoints=1, $localized=true, $stripzeros=fal } else { $separator = '.'; } + if ($decimalpoints == -1) { + // The following counts the number of decimals. + // It is safe as both floatval() and round() functions have same behaviour when non-numeric values are provided. + $floatval = floatval($float); + for ($decimalpoints = 0; $floatval != round($float, $decimalpoints); $decimalpoints++); + } + $result = number_format($float, $decimalpoints, $separator, ''); if ($stripzeros) { // Remove zeros and final dot if not needed. diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index b65af4012b3..82db58e3059 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -2249,6 +2249,14 @@ class core_moodlelib_testcase extends advanced_testcase { // Custom number of decimal places. $this->assertEquals('5.43000', format_float(5.43, 5)); + // Auto detect the number of decimal places. + $this->assertEquals('5.43', format_float(5.43, -1)); + $this->assertEquals('5.43', format_float(5.43000, -1)); + $this->assertEquals('5', format_float(5, -1)); + $this->assertEquals('5', format_float(5.0, -1)); + $this->assertEquals('0.543', format_float('5.43e-1', -1)); + $this->assertEquals('0.543', format_float('5.43000e-1', -1)); + // Option to strip ending zeros after rounding. $this->assertEquals('5.43', format_float(5.43, 5, true, true)); $this->assertEquals('5', format_float(5.0001, 3, true, true)); diff --git a/lib/upgrade.txt b/lib/upgrade.txt index b65a321b348..9ea25900d87 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -1,6 +1,9 @@ This files describes API changes in core libraries and APIs, information provided here is intended especially for developers. +=== 3.8.1 === +format_float() now accepts a special value (-1) as the $decimalpoints parameter which means auto-detecting number of decimal points. + === 3.8 === * The rotate_image function has been added to the stored_file class (MDL-63349) * The yui checknet module is removed. Call \core\session\manager::keepalive instead.