mirror of
https://github.com/moodle/moodle.git
synced 2025-04-18 06:58:08 +02:00
MDL-67151 core: format_float can auto detect number of decimal points
This commit is contained in:
parent
800563e415
commit
ac384d1d88
@ -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.
|
||||
|
@ -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));
|
||||
|
@ -1,6 +1,9 @@
|
||||
This files describes API changes in core libraries and APIs,
|
||||
information provided here is intended especially for developers.
|
||||
|
||||
=== 3.9 ===
|
||||
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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user