Commit fbc8c141 by pupi1985

Updated qa_format_number function to support compact numbers

parent 17f95d94
...@@ -2046,21 +2046,45 @@ ...@@ -2046,21 +2046,45 @@
} }
/** /**
* Format a number using the decimal point and thousand separator specified in the language files * Format a number using the decimal point and thousand separator specified in the language files. If the number
* is compacted it is turned into a string such as 1.3k or 2.5m
* @param integer $number Number to be formatted * @param integer $number Number to be formatted
* @param integer $decimals Amount of decimals to use * @param integer $decimals Amount of decimals to use
* @param bool $compact Whether to show compact numbers or not
* @return string The formatted number as a string * @return string The formatted number as a string
*/ */
function qa_format_number($number, $decimals = 0) function qa_format_number($number, $decimals = 0, $compact = false)
{ {
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); } if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
$suffix = '';
if ($compact) {
if ($number != 0) {
$base = log($number) / log(1000);
$floorBase = floor($base);
$number = round(pow(1000, $base - $floorBase), 1);
// If $number is too long then remove the decimals, e.g., 123k instead of 123.4k
if ($number >= 100) {
$decimals = 0;
}
// If $number exceeds millions then don't add any suffix
$suffixes = array('', qa_lang_html('main/_thousands_suffix'), qa_lang_html('main/_millions_suffix'));
$suffix = isset($suffixes[$floorBase]) ? $suffixes[$floorBase] : '';
}
// If the decimal part is 0 then remove it
if ($number == (int) $number) {
$decimals = 0;
}
} else {
$decimals = 0;
}
return number_format( return number_format(
$number, $number,
$decimals, $decimals,
qa_lang_html('main/_decimal_point'), qa_lang_html('main/_decimal_point'),
qa_lang_html('main/_thousands_separator') qa_lang_html('main/_thousands_separator')
); ) . $suffix;
} }
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
return array( return array(
'_decimal_point' => '.', '_decimal_point' => '.',
'_thousands_separator' => ',', '_thousands_separator' => ',',
'_thousands_suffix' => 'k',
'_millions_suffix' => 'm',
'1_answer' => '1 answer', '1_answer' => '1 answer',
'1_comment' => '1 comment', '1_comment' => '1 comment',
'1_day' => '1 day', '1_day' => '1 day',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment