1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
<?php namespace amekusa\plz; main::required;
/**
* Number utilities
*
* To get started, place the following line around top of your code.
* ```php
* use amekusa\plz\num;
* ```
*/
abstract class num {
/**
* Returns an ordinal number
* @example Demonstration
* ```php
* var_dump( num::ordinal(1) ); // First
* var_dump( num::ordinal(2) ); // Second
* var_dump( num::ordinal(3) ); // Third
* var_dump( num::ordinal(4) ); // Fourth
* var_dump( num::ordinal(11) ); // Eleventh
* var_dump( num::ordinal(20) ); // Twentieth
* var_dump( num::ordinal(21) ); // Twenty-first
* var_dump( num::ordinal(-1) ); // Negative first
* ```
* ```php
* string(3) "1st"
* string(3) "2nd"
* string(3) "3rd"
* string(3) "4th"
* string(4) "11th"
* string(4) "20th"
* string(4) "21st"
* string(4) "-1st"
* ```
* @param integer $X A number
* @return string
*/
static function ordinal($X) {
$n = abs($X) % 100;
if ($n < 21 && $n > 3) $suffix = 'th';
else {
switch ($n % 10) {
case 1:
$suffix = 'st';
break;
case 2:
$suffix = 'nd';
break;
case 3:
$suffix = 'rd';
break;
default:
$suffix = 'th';
}
}
return $X.$suffix;
}
/**
* Returns an alphabetic character indexed by `$X`
* @example Demonstration
* ```php
* var_dump( num::abc(0) ); // 'a'
* var_dump( num::abc(1) ); // 'b'
* var_dump( num::abc(2) ); // 'c'
* var_dump( num::abc(25) ); // 'z'
* var_dump( num::abc(26) ); // 'a'
* ```
* ```php
* string(1) "a"
* string(1) "b"
* string(1) "c"
* string(1) "z"
* string(1) "a"
* ```
* @param integer $X A number
* @return string
*/
static function abc($X) {
static $map = null;
if (!$map) $map = range('a', 'z');
return $map[$X % 26];
}
}