diff --git a/.github/workflows/plugin-ci-workflow.yml b/.github/workflows/plugin-ci-workflow.yml index e806000..02b1688 100644 --- a/.github/workflows/plugin-ci-workflow.yml +++ b/.github/workflows/plugin-ci-workflow.yml @@ -85,7 +85,7 @@ jobs: run: sudo apt-get update - name: Install System Dependencies - run: sudo apt-get install -y apache2 snmp snmpd rrdtool fping libapache2-mod-php${{ matrix.php }} + run: sudo apt-get install -y apache2 snmp snmpd rrdtool fping - name: Start SNMPD Agent and Test run: | diff --git a/lib/WeatherMap.functions.php b/lib/WeatherMap.functions.php index 90ef0c6..3aa5d5c 100644 --- a/lib/WeatherMap.functions.php +++ b/lib/WeatherMap.functions.php @@ -1640,11 +1640,12 @@ function format_number($number, $precision = 2, $trailing_zeroes = 0) { $sign = -1; } - $number = round($number, $precision); + $number = (string) round($number, $precision); $integer = intval($number); + $integer_string = (string) $integer; - if (strlen($integer) < strlen($number)) { - $decimal = substr($number, strlen($integer) + 1); + if (strlen($integer_string) < strlen($number)) { + $decimal = substr($number, strlen($integer_string) + 1); } $decimal ??= ''; diff --git a/tests/Unit/FormatNumberTest.php b/tests/Unit/FormatNumberTest.php new file mode 100644 index 0000000..1e3e4ab --- /dev/null +++ b/tests/Unit/FormatNumberTest.php @@ -0,0 +1,16 @@ +toBe('12.35'); + }); + + it('formats whole numbers without a decimal suffix', function (): void { + expect(format_number(12.0, 2))->toBe(12); + }); +});