diff --git a/CHANGELOG.md b/CHANGELOG.md index 782445e..c32b22c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/). +## 1.5.1 + ++ [#19](https://github.com/luyadev/yii-helpers/pull/19) Fixed issue with ordinal numbers. + ## 1.5.0 (26. October 2023) + Added new `StringHelper::toYouTubeEmbed()` function to extract YouTube links into an Embed links. diff --git a/src/helpers/StringHelper.php b/src/helpers/StringHelper.php index fa18b90..e040520 100644 --- a/src/helpers/StringHelper.php +++ b/src/helpers/StringHelper.php @@ -173,6 +173,11 @@ public static function isFloat($value) return true; } + if (!is_array($value) && preg_match('/^\d+\.$/', $value)) { + // ordinal number of the form cardinal number followed by point, e.g. "24." + return false; + } + return ($value == (string)(float) $value); } diff --git a/tests/helpers/StringHelperTest.php b/tests/helpers/StringHelperTest.php index 788da4a..6778ad4 100644 --- a/tests/helpers/StringHelperTest.php +++ b/tests/helpers/StringHelperTest.php @@ -38,6 +38,9 @@ public function testIsFloat() $this->assertTrue(StringHelper::isFloat('-1')); $float = 1.0; $this->assertTrue(StringHelper::isFloat($float)); + + $this->assertTrue(StringHelper::isFloat('.5')); + $this->assertFalse(StringHelper::isFloat('5.')); $this->assertFalse(StringHelper::isFloat('string')); } @@ -53,6 +56,9 @@ public function testTypeCastNumeric() $this->assertSame(1.5, StringHelper::typeCastNumeric(1.5)); $this->assertSame(-1, StringHelper::typeCastNumeric(-1)); $this->assertSame(-1.5, StringHelper::typeCastNumeric(-1.5)); + + $this->assertSame(0.5, StringHelper::typeCastNumeric('.5')); + $this->assertSame('5.', StringHelper::typeCastNumeric('5.')); $this->assertSame(1, StringHelper::typeCastNumeric(true)); $this->assertSame(0, StringHelper::typeCastNumeric(false));