Added number utility class and a method to add ordinal indicators to a number (and without the need of PHP 5.3 which has some new class for that)

This commit is contained in:
Josh Sherman 2012-11-17 12:27:00 -05:00
parent 6f80fb51df
commit 29c0dbad1f
2 changed files with 119 additions and 0 deletions

61
classes/Number.php Normal file
View file

@ -0,0 +1,61 @@
<?php
/**
* Number Utility Collection
*
* PHP version 5
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <pickles@joshtronic.com>
* @copyright Copyright 2007-2012, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES
* @link https://github.com/joshtronic/pickles
*/
/**
* Number Class
*
* Just a simple collection of static functions to accomplish some of the more
* redundant numeric related manipulation.
*/
class Number
{
/**
* Ordinal Indiciator
*
* Formats a number by appending an ordinal indicator.
*
* @static
* @link http://en.wikipedia.org/wiki/Ordinal_indicator
* @link http://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers
* @param string $number number to format
* @param boolean $superscript include <sup> tags
* @return string formatted number
*/
public static function ordinalIndicator($number, $superscript = false)
{
$suffix = 'th';
if (!in_array(($number % 100), array(11, 12, 13)))
{
switch ($number % 10)
{
case 1: $suffix = 'st'; break;
case 2: $suffix = 'nd'; break;
case 3: $suffix = 'rd'; break;
}
}
if ($superscript)
{
$suffix = '<sup>' . $suffix . '</sup>';
}
return $number . $suffix;
}
}
?>

58
jar.php
View file

@ -5788,6 +5788,64 @@ class Module extends Object
}
}
/**
* Number Utility Collection
*
* PHP version 5
*
* Licensed under The MIT License
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <pickles@joshtronic.com>
* @copyright Copyright 2007-2012, Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @package PICKLES
* @link https://github.com/joshtronic/pickles
*/
/**
* Number Class
*
* Just a simple collection of static functions to accomplish some of the more
* redundant numeric related manipulation.
*/
class Number
{
/**
* Ordinal Indiciator
*
* Formats a number by appending an ordinal indicator.
*
* @static
* @link http://en.wikipedia.org/wiki/Ordinal_indicator
* @link http://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers
* @param string $number number to format
* @param boolean $superscript include <sup> tags
* @return string formatted number
*/
public static function ordinalIndicator($number, $superscript = false)
{
$suffix = 'th';
if (!in_array(($number % 100), array(11, 12, 13)))
{
switch ($number % 10)
{
case 1: $suffix = 'st'; break;
case 2: $suffix = 'nd'; break;
case 3: $suffix = 'rd'; break;
}
}
if ($superscript)
{
$suffix = '<sup>' . $suffix . '</sup>';
}
return $number . $suffix;
}
}
/**
* Object Class File for PICKLES
*