Created
August 21, 2019 23:22
-
-
Save wearehyphen/fe4c5f6efe99d26a48adf6d52c8567f6 to your computer and use it in GitHub Desktop.
Integer → String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Usage: | |
require( TEMPLATEPATH . '/includes/Int_String.php' ); | |
$intstr = new IntString; | |
echo $intstr->int2str( 123 ); // 123 → One Hundred and Twenty Three | |
**/ | |
/** | |
* Demo library class intended to be added to in the future | |
*/ | |
class IntString { | |
/***** NOTE: a const cannot be an array in PHP, so making these arrays static is the next best thing *****/ | |
/** | |
* @var array $_numbersUnder20 Array containing the word associated with the index's number value | |
*/ | |
private static $_numbersUnder20 = [ | |
'zero', 'one', 'two', 'three', 'four', | |
'five', 'six', 'seven', 'eight', 'nine', | |
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', | |
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' | |
]; | |
/** | |
* @var array $_tensDigits Array containing all tens digit values except 10 | |
*/ | |
private static $_tensDigits = [ | |
'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' | |
]; | |
/** | |
* @var array $_orderOfMagnitude Array containing the higher-order digit values; can also be | |
* thought of as the order of magnitude of the target digit | |
*/ | |
private static $_orderOfMagnitude = [ | |
// Stopped at "quintillion" because the maximum PHP int value on 64-bit Linux is | |
// 9,223,372,036,854,775,807 (a.k.a. 2^63 - 1 because PHP doesn't support unsigned ints) | |
'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion' | |
]; | |
/** | |
* Recursively calculates the string-, word-based equivalent of the target integer | |
* | |
* @param integer $num Integer whose value will be converted to a word-based string | |
* @param boolean $recursive Determines if the currently-executing code is being called | |
* recursively; allows for non-recursive 0 to be converted to "zero" | |
* otherwise use an empty string | |
* | |
* @throws InvalidArgumentException if the first argument is not of type int | |
* | |
* @return string Partially- or fully-built word-based representation of the target integer | |
*/ | |
private function _integerToWords($num, $recursive=false) { | |
// Ensure a valid integer | |
if(!is_int($num)) { | |
throw new InvalidArgumentException( | |
__FUNCTION__ . ' expects parameter 1 to be of type integer; actual type: ' . | |
gettype($num) | |
); | |
} | |
/***** Perform the int to string conversion based on the size of $num *****/ | |
// Negative | |
if($num < 0) { | |
return 'negative ' . $this->_integerToWords(-1 * $num, true); | |
} | |
// 0 or no value in the lowest digits | |
if($num === 0) { | |
return $recursive ? '' : 'zero'; | |
} | |
// 1-19 | |
if($num < 20) { | |
return self::$_numbersUnder20[$num]; | |
} | |
// 20 - 99 | |
if($num < 100) { | |
$highDigitValue = intval(floor($num / 10) - 2); // Value of the highest-order digit | |
$remainingValue = $num % 10; // Value of the remaining digits | |
return | |
self::$_tensDigits[$highDigitValue] . | |
'-' . | |
$this->_integerToWords($remainingValue, true); | |
} | |
// 100 - 999 | |
if($num < 1000) { | |
$highDigitValue = intval(floor($num / 100)); // Value of the highest-order digit | |
$remainingValue = $num % 100; // Value of the remaining digits | |
if( $remainingValue === 0 ) { | |
return | |
$this->_integerToWords($highDigitValue,true) . | |
'-hundred'; | |
} else { | |
return | |
$this->_integerToWords($highDigitValue, true) . | |
'-hundred and ' . | |
$this->_integerToWords($remainingValue, true); | |
} | |
} | |
// 1,000+ | |
$quotient = $num; | |
$divideCount = 0; | |
while($quotient >= 1000) { | |
$quotient /= 1000; | |
++$divideCount; | |
} | |
$highDigitValue = intval(floor($quotient)); // Value of the highest-order digit | |
$remainingValue = $num - ($highDigitValue * pow(1000, $divideCount)); // Value of the remaining digits | |
return | |
$this->_integerToWords($highDigitValue, true) . | |
'-' . | |
self::$_orderOfMagnitude[$divideCount - 1] . | |
' ' . | |
$this->_integerToWords($remainingValue, true); | |
} | |
/** | |
* @api | |
* | |
* Calculates the string-, word-based equivalent of the target integer | |
* | |
* @param integer $num Integer whose value will be converted to a word-based string | |
* | |
* @return string Fully-built word-based representation of the target integer | |
*/ | |
public function int2str($num) { | |
return trim($this->_integerToWords($num), "- \t\n\r\0\x0B"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment