MyNumber = 1,234,567.89
£[[$MyNumber]]
£[[$MyNumber:numberFormat=`nearestHundredThousand`]]
£[[$MyNumber:numberFormat=`nearestHundred`]]
This question has been answered by clareoconsulting. See the first response.
<?php
$number = floatval($input);
$optionsXpld = @explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = @explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;
[[$bigNumber:numberFormat=`&decimals=2&dec_point=.&thousands_sep=,`]]
I'm not too good with php yet, but just looking at what you are trying to achieve without discussing the code, if you Round 1.29 the correct answer would be 1.3. Perhaps rather than Rounding you want to look for something that Truncates the number?
<?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;
$[[++Assets:fmt.humanNumber]]
[[++Financial_Benefits:fmt.humanNumber=`stripWords=1&decimals=1`]]
<?php
$optionsXpld = @explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = @explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$stripWords = isset($optionsArray['stripWords']) ? $optionsArray['stripWords'] : null;
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : 0;
# Output easy-to-read numbers
# by james at bandit.co.nz
$number = $input;
// first strip any formatting;
$n = (0+str_replace(",","",$number));
// is this a number?
if(!is_numeric($n)) return false;
// now filter it;
if ($n>1000000000000):
$num = round(($n/1000000000000),1);
$wrd = 'trillion';
elseif ($n>1000000000):
$num = round(($n/1000000000),1);
$wrd = 'billion';
elseif ($n>1000000):
$num = round(($n/1000000),1);
$wrd = 'million';
elseif ($n>1000):
$num = round(($n/1000),1);
$wrd = 'thousand';
elseif ($n>100):
$num = round(($n/100),1);
$wrd = 'hundred';
endif;
if($stripWords=1){
$output = number_format($num, $decimals);
}else{
$output = number_format($num, $decimals) . " " . $wrd;
}
return $output;