note 55316 added to function.is-numeric
| From: | namik at hub-cafe dot net | Date: | Sat, 30 Jul 2005 01:11:17 +0000 |
| Subject: | note 55316 added to function.is-numeric | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92748@lists.php.net to get a copy of this message | ||
I needed a number_suffix function that takes numbers with thousand seperators (using number_format()
function). Note that this doesn't properly handle decimals.
Example:
<?= number_suffix('1,021') ?> returns: 1,021st
Also, increasing the range above the condition statements increases efficiency. That's almost
20% of the numbers between 0 and 100 that get to end early.
<?
function number_suffix($number)
{
// Validate and translate our input
if ( is_numeric($number) )
{
// Get the last two digits (only once)
$n = $number % 100;
} else {
// If the last two characters are numbers
if ( preg_match( '/[0-9]?[0-9]$/', $number, $matches ) )
{
// Return the last one or two digits
$n = array_pop($matches);
} else {
// Return the string, we can add a suffix to it
return $number;
}
}
// Skip the switch for as many numbers as possible.
if ( $n > 3 && $n < 21 )
return $number . 'th';
// Determine the suffix for numbers ending in 1, 2 or 3, otherwise add a 'th'
switch ( $n % 10 )
{
case '1': return $number . 'st';
case '2': return $number . 'nd';
case '3': return $number . 'rd';
default: return $number . 'th';
}
}
?>
----
Submitter IP: 64.246.30.37
----
Manual Page -- http://www.php.net/manual/en/function.is-numeric.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55316
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55316&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55316&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55316&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55316&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55316&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55316&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55316&report=yes
Search -- http://master.php.net/manage/user-notes.php