Skip to content

Commit

Permalink
Merge pull request #19 from hbugdoll/patch-1
Browse files Browse the repository at this point in the history
Fixed issue with ordinal numbers
  • Loading branch information
nadar authored Jan 9, 2024
2 parents 006f1e7 + de5a226 commit df002ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand All @@ -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));
Expand Down

0 comments on commit df002ac

Please sign in to comment.