note 51089 deleted from function.is-numeric by cmb
| From: | cmb@php.net | Date: | Sun, 23 Jun 2019 10:35:02 +0000 |
| Subject: | note 51089 deleted from function.is-numeric by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211349@lists.php.net to get a copy of this message | ||
Note Submitter: lukesneeringer at gmail dot com
----
Regarding renimar at yahoo's function to yield ordinal numbers, the function lacks one thing.
It accounts for numbers in the teens only if the number is below 100. If you used this function and
gave 212 as the input, it would give 212nd, and not 212th. (Also, checking for numbers between 11
and 13 is sufficient, since 14-19 yield th either way.)
Therefore,
<?php if ($num >= 11 and $num <= 19) ?>
should be changed to...
<?php if ($num % 100>= 11 and $num % 100 <= 13) ?>
It will then work perfectly all the time.
Here's the entire function with the one line changed:
<?php
function ordinalize($num) {
if (!is_numeric($num))
return $num;
if ($num % 100 >= 11 and $num % 100 <= 13)
return $num."th";
elseif ( $num % 10 == 1 )
return $num."st";
elseif ( $num % 10 == 2 )
return $num."nd";
elseif ( $num % 10 == 3 )
return $num."rd";
else // $num % 10 == 0, 4-9
return $num."th";
}
?>