<!--?php
// Configure the following 5 arrays to control output based on
// the number of digits in the input number
// Cases where output will be displayed in multiples, i.e. "5 million"
// instead of "5,000,000". Enter the number to divide by (eg. 1000000 for a million,
// 1000 for a thousand, 1 for no division)
$displayDivisor = array(
1 =--> 1,
2 => 1,
3 => 1,
4 => 1,
5 => 1,
6 => 1,
7 => 1000000,
8 => 1000000,
9 => 1000000
);
// Precision of rounding of input. Eg. 3 rounds at thousands
// 6 rounds at millions, 5 rounds to tenth of million, etc.
$roundingDigits = array(
1 => 0,
2 => 0,
3 => 0,
4 => 2,
5 => 3,
6 => 3,
7 => 5,
8 => 6,
9 => 6
);
// How many decimal places to display in the output
$displayDecimals = array(
1 => 0,
2 => 0,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 1,
8 => 0,
9 => 0
);
// Text prefix for output
$displayPrefix = array(
1 => '',
2 => '',
3 => '',
4 => 'more than ',
5 => 'more than ',
6 => 'more than ',
7 => 'more than ',
8 => 'more than ',
9 => 'more than '
);
// Text suffix for output
$displaySuffix = array(
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
6 => '',
7 => ' million',
8 => ' million',
9 => ' million'
);
// CHANGE NOTHING AFTER THIS POINT
$value = floatval($input);
$digitCount = intval(log10($value)) + 1;
if (! isset($displayDivisor[$digitCount]) ||
! isset($roundingDigits[$digitCount]) ||
! isset($displayDecimals[$digitCount]) ||
! isset($displayPrefix[$digitCount]) ||
! isset($displaySuffix[$digitCount])
) { return ''; }
$roundingPrecision = pow(10, $roundingDigits[$digitCount]);
$floorValue = floor($value / $roundingPrecision) * $roundingPrecision / $displayDivisor[$digitCount];
$outputValue = number_format($floorValue, $displayDecimals[$digitCount]);
$output = $displayPrefix[$digitCount] .
$outputValue .
$displaySuffix[$digitCount];
$return $output;