diff --git a/lib/moodlelib.php b/lib/moodlelib.php index a0a39373095..9a1728771c9 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8767,8 +8767,9 @@ function format_float($float, $decimalpoints=1, $localized=true, $stripzeros=fal } $result = number_format($float, $decimalpoints, $separator, ''); - if ($stripzeros) { + if ($stripzeros && $decimalpoints > 0) { // Remove zeros and final dot if not needed. + // However, only do this if there is a decimal point! $result = preg_replace('~(' . preg_quote($separator, '~') . ')?0+$~', '', $result); } return $result; diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 13a70526d0c..57d9cc393be 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -2340,6 +2340,8 @@ EOF; // 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)); + $this->assertEquals('100', format_float(100, 2, true, true)); + $this->assertEquals('100', format_float(100, 0, true, true)); // Tests with a localised decimal separator. $this->define_local_decimal_separator();