MDL-67151 forms: Support for the scientific format in the float element

This commit is contained in:
Shamim Rezaie 2019-11-22 16:50:23 +11:00
parent ac384d1d88
commit 2bfc16ad2a

View File

@ -175,12 +175,18 @@ class MoodleQuickForm_float extends MoodleQuickForm_text {
*/
private function format_float($value) {
if (is_numeric($value)) {
if ($value > 0) {
$decimals = strlen($value) - strlen(floor($value)) - 1;
// We want to keep trailing zeros after the decimal point if there is any.
// Therefore we cannot just call format_float() and pass -1 as the number of decimal points.
$pieces = preg_split('/E/i', $value); // In case it is in the scientific format.
$decimalpos = strpos($pieces[0], '.');
if ($decimalpos !== false) {
$decimalpart = substr($pieces[0], $decimalpos + 1);
$decimals = strlen($decimalpart);
} else {
$decimals = strlen($value) - strlen(ceil($value)) - 1;
$decimals = 0;
}
$value = format_float($value, $decimals);
$pieces[0] = format_float($pieces[0], $decimals);
$value = implode('E', $pieces);
}
return $value;
}