Commit 8f780c62 by Scott

Fix incorrect output from qa_format_number

Decimals for non-compact numbers were being incorrectly removed.
Numbers were being rounded twice (123456789 -> 124k not 123k).
Numbers larger than 1 billion were being incorrectly shortened.
parent 3afd3876
......@@ -2046,11 +2046,12 @@
}
/**
* 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
* 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 $decimals Amount of decimals to use
* @param bool $compact Whether to show compact numbers or not
* @param integer $decimals Amount of decimals to use (ignored if number gets shortened)
* @param bool $compact Whether the number can be shown as compact or not
* @return string The formatted number as a string
*/
function qa_format_number($number, $decimals = 0, $compact = false)
......@@ -2058,28 +2059,25 @@
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
$suffix = '';
if ($compact && qa_opt('show_compact_numbers')) {
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;
// only the k/m cases are currently supported (i.e. no billions)
if ($number >= 1000000) {
$number /= 1000000;
$suffix = qa_lang_html('main/_millions_suffix');
}
// 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] : '';
elseif ($number >= 1000) {
$number /= 1000;
$suffix = qa_lang_html('main/_thousands_suffix');
}
// If the decimal part is 0 then remove it
if ($number == (int) $number) {
$decimals = 0;
// keep decimal part if not 0 and number is short (e.g. 9.1k)
$rounded = round($number, 1);
if ($number < 100 && ($rounded != (int)$rounded)) {
$decimals = 1;
}
}
else {
$decimals = 0;
}
return number_format(
$number,
......
......@@ -16,14 +16,17 @@ class AppFormatTest extends PHPUnit_Framework_TestCase
$qa_phrases_custom['main']['_decimal_point'] = '.';
$qa_phrases_custom['main']['_thousands_separator'] = ',';
$this->assertSame( '5.5', qa_format_number(5.452, 1) );
$this->assertSame( '5', qa_format_number(5.452, 0) );
$this->assertSame('5.5', qa_format_number(5.452, 1));
$this->assertSame('5', qa_format_number(5.452, 0));
$this->assertSame('5', qa_format_number(4.5, 0));
$this->assertSame('9,123', qa_format_number(9123, 0));
$this->assertSame('9,123.0', qa_format_number(9123, 1));
// $compact parameter should have no effect here
$this->assertSame('9,123.0', qa_format_number(9123, 1, true));
// not shortened unless 'show_compact_numbers' is true
$this->assertSame('5.0', qa_format_number(5, 1, true));
$this->assertSame('5.5', qa_format_number(5.452, 1, true));
$this->assertSame('5', qa_format_number(5.452, 0, true));
$this->assertSame('9,123', qa_format_number(9123, 0, true));
$this->assertSame('123,456,789', qa_format_number(123456789, 0, true));
// change separators
......@@ -50,25 +53,34 @@ class AppFormatTest extends PHPUnit_Framework_TestCase
$qa_phrases_custom['main']['_thousands_suffix'] = 'k';
$qa_phrases_custom['main']['_millions_suffix'] = 'm';
// $decimal parameter ignored when 'show_compact_numbers' is true
$this->assertSame('5.5', qa_format_number(5.452, 0, true));
$this->assertSame('5.5', qa_format_number(5.452, 1, true));
$this->assertSame('5', qa_format_number(5, 1, true));
$this->assertSame('9.1k', qa_format_number(9123, 0, true));
$this->assertSame('9.1k', qa_format_number(9123, 1, true));
$this->assertSame('9k', qa_format_number(9123, 0, true));
$this->assertSame('9k', qa_format_number(9000, 0, true));
$this->assertSame('9k', qa_format_number(9000, 1, true));
$this->assertSame('9k', qa_format_number(9040, 0, true));
$this->assertSame('9k', qa_format_number(9040, 1, true));
$this->assertSame('9.1k', qa_format_number(9050, 0, true));
$this->assertSame('123m', qa_format_number(123456789, 0, true));
$this->assertSame('235m', qa_format_number(234567891, 0, true));
$this->assertSame('23.5m', qa_format_number(23456789, 1, true));
$this->assertSame('123m', qa_format_number(123456789, 1, true));
$this->assertSame('235m', qa_format_number(234567891, 1, true));
$this->assertSame('1,223m', qa_format_number(1223456789, 0, true));
$this->assertSame('9,000', qa_format_number(9000, 0, false));
$this->assertSame('912.3', qa_format_number(912.3, 1, false));
$this->assertSame('123,456,789', qa_format_number(123456789, 0, false));
// change compact suffixes
// change separators and compact suffixes
$qa_phrases_custom['main']['_decimal_point'] = ',';
$qa_phrases_custom['main']['_thousands_separator'] = '.';
$qa_phrases_custom['main']['_thousands_suffix'] = 'th';
$qa_phrases_custom['main']['_millions_suffix'] = 'mi';
$this->assertSame('9.1th', qa_format_number(9123, 1, true));
$this->assertSame('9th', qa_format_number(9123, 0, true));
$this->assertSame('9,1th', qa_format_number(9123, 0, true));
$this->assertSame('123mi', qa_format_number(123456789, 0, true));
}
}
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