Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with ordinal numbers #19

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading