diff --git a/customfield/field/number/classes/field_controller.php b/customfield/field/number/classes/field_controller.php
index a5a881ff592..8b5318b810d 100644
--- a/customfield/field/number/classes/field_controller.php
+++ b/customfield/field/number/classes/field_controller.php
@@ -143,18 +143,21 @@ class field_controller extends \core_customfield\field_controller {
* @return string|null
*/
public function prepare_field_for_display(mixed $value, ?context $context = null): ?string {
- if ((float) $value == 0) {
+ if ($value === null) {
+ return null;
+ }
+ $decimalplaces = (int) $this->get_configdata_property('decimalplaces');
+ if (round((float) $value, $decimalplaces) == 0) {
$value = $this->get_configdata_property('displaywhenzero');
if ((string) $value === '') {
return null;
}
} else {
// Let's format the value.
- $decimalplaces = (int) $this->get_configdata_property('decimalplaces');
$value = format_float((float) $value, $decimalplaces);
// Apply the display format.
- $format = $this->get_configdata_property('display');
+ $format = $this->get_configdata_property('display') ?? '{value}';
$value = str_replace('{value}', $value, $format);
}
return format_string($value, true, ['context' => $context ?? system::instance()]);
diff --git a/customfield/field/number/tests/data_controller_test.php b/customfield/field/number/tests/data_controller_test.php
index b1640ad4eb9..469f41cfd64 100644
--- a/customfield/field/number/tests/data_controller_test.php
+++ b/customfield/field/number/tests/data_controller_test.php
@@ -129,7 +129,7 @@ final class data_controller_test extends advanced_testcase {
$template = '$ {value}€ {value}';
$whenzero = 'UnknownDesconocido';
return [
- 'Export float value' => [42, 42.0, [
+ 'Export float value' => [42, '42.00', [
'decimalplaces' => 2,
'display' => '{value}',
'displaywhenzero' => 0,
@@ -143,6 +143,22 @@ final class data_controller_test extends advanced_testcase {
'display' => '{value}',
'displaywhenzero' => $whenzero,
]],
+ 'Export value when not set' => ['', null, [
+ 'display' => '{value}',
+ 'displaywhenzero' => $whenzero,
+ ]],
+ 'Export almost zero that rounds to non-zero' => [0.0009, '0.001', [
+ 'decimalplaces' => 3,
+ 'display' => '{value}',
+ 'displaywhenzero' => 'Free',
+ ]],
+ 'Export almost zero that rounds to zero' => [0.0004, 'Free', [
+ 'decimalplaces' => 3,
+ 'display' => '{value}',
+ 'displaywhenzero' => 'Free',
+ ]],
+ 'Export when config not set' => [42, '42', []],
+ 'Export zero when config not set' => [0, null, []],
];
}
@@ -150,14 +166,14 @@ final class data_controller_test extends advanced_testcase {
* Test exporting instance
*
* @param float|string $datavalue
- * @param float|string $expectedvalue
+ * @param string|null $expectedvalue
* @param array $configdata
*
* @dataProvider export_value_provider
*/
public function test_export_value(
float|string $datavalue,
- float|string $expectedvalue,
+ string|null $expectedvalue,
array $configdata,
): void {
$this->resetAfterTest();
@@ -181,6 +197,6 @@ final class data_controller_test extends advanced_testcase {
$data = $generator->add_instance_data($field, (int) $course->id, $datavalue);
$result = \core_customfield\data_controller::create($data->get('id'))->export_value();
- $this->assertEquals($expectedvalue, $result);
+ $this->assertSame($expectedvalue, $result);
}
}