samjaninf
9/26/2018 - 7:18 PM

numbers_to_human_readable.php

// https://inkplant.com/code/spell-numbers

<?php

function spellnumber($x,$min=0,$max=100) {
	if (!preg_match('/^-?([0-9]{1,15})(\.[0-9]+)?$/',$x)) { return $x; } //if not a number less than a quadrillion, leave it alone
	elseif (strpos($x,'.') !== false) { list($int,$dec) = explode('.',$x); return number_format($int).'.'.$dec; } //if not an intenger
	elseif (($min !== false) && ($x < $min)) { return number_format($x); } //return numeral if less than minimum
	elseif (($max !== false) && ($x > $max)) { return number_format($x); } //return numeral if greater than maximum
	
	if ($x < 0) { $w = 'negative '; $x = abs($x); } else { $w = ''; } //check to see if it's negative

	if ($x < 20) { return $w.spellnumber_under20($x); } //shortcut for small numbers

	$d = str_split($x); //explode the number into an array of single digits
	$d = array_reverse($d); //reverse so that we can work from the right
	
	//trillions
	if (array_key_exists('12',$d)) {
		$t = floor($x/1000000000000);
		if ($t > 0) { $w .= spellnumber($t,0,999).' trillion '; }
		$x = $x - ($t * 1000000000000);
	}

	//billions
	if (array_key_exists('9',$d)) {
		$t = floor($x/1000000000);
		if ($t > 0) { $w .= spellnumber($t,0,999).' billion '; }
		$x = $x - ($t * 1000000000);
	}
	
	//millions
	if (array_key_exists('6',$d)) {
		$t = floor($x/1000000);
		if ($t > 0) { $w .= spellnumber($t,0,999).' million '; }
		$x = $x - ($t * 1000000);
	}

	//thousands
	if (array_key_exists('3',$d)) {
		$t = floor($x/1000);
		if ($t > 0) { $w .= spellnumber($t,0,999).' thousand '; }
	}

	//third-to-last digit
	if (array_key_exists('2',$d)) {
		if ($d['2'] == 1) { $w .= 'one hundred '; }
		elseif ($d['2'] == 2) { $w .= 'two hundred '; }
		elseif ($d['2'] == 3) { $w .= 'three hundred '; }
		elseif ($d['2'] == 4) { $w .= 'four hundred '; }
		elseif ($d['2'] == 5) { $w .= 'five hundred '; }
		elseif ($d['2'] == 6) { $w .= 'six hundred '; }
		elseif ($d['2'] == 7) { $w .= 'seven hundred '; }
		elseif ($d['2'] == 8) { $w .= 'eight hundred '; }
		elseif ($d['2'] == 9) { $w .= 'nine hundred '; }
	}

	$last2 = round($d['1'].$d['0']); //combine the last 2 digits and handle them together
	if ($last2 < 20) {
		if ($last2 > 0) { $w .= spellnumber_under20($last2); }
	} else {
		//second-to-last digit
		if ($d['1'] == 2) { $w .= 'twenty'; }
		elseif ($d['1'] == 3) { $w .= 'thirty'; }
		elseif ($d['1'] == 4) { $w .= 'forty'; }
		elseif ($d['1'] == 5) { $w .= 'fifty'; }
		elseif ($d['1'] == 6) { $w .= 'sixty'; }
		elseif ($d['1'] == 7) { $w .= 'seventy'; }
		elseif ($d['1'] == 8) { $w .= 'eighty'; }
		elseif ($d['1'] == 9) { $w .= 'ninety'; }

		//last digit
		if ($d['0'] != 0) {
			if ($d['1'] >= 2) { $w .= '-'; } //if 20 or more
			$w .= spellnumber_under20($d['0']);
		}
	}
	
	return $w;
}

//this next function does not have any input verification. it should only be called from within the main spellnumber function
function spellnumber_under20($x) {
	switch($x) {
		case 0; return 'zero';
		case 1; return 'one';
		case 2; return 'two';
		case 3; return 'three';
		case 4; return 'four';
		case 5; return 'five';
		case 6; return 'six';
		case 7; return 'seven';
		case 8; return 'eight';
		case 9; return 'nine';
		case 10; return 'ten';
		case 11; return 'eleven';
		case 12; return 'twelve';
		case 13; return 'thirteen';
		case 14; return 'fourteen';
		case 15; return 'fifteen';
		case 16; return 'sixteen';
		case 17; return 'seventeen';
		case 18; return 'eighteen';
		case 19; return 'nineteen';
	}
	return $x; //just in case
}

?>