Commit 08963093 by Scott

Improve compact numbers

Fixes #781
parent 0f7e1461
...@@ -2359,7 +2359,8 @@ function qa_favorite_form($entitytype, $entityid, $favorite, $title) ...@@ -2359,7 +2359,8 @@ function qa_favorite_form($entitytype, $entityid, $favorite, $title)
/** /**
* 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. * If the number is compacted it is turned into a string such as 17.3k (only the thousand/million
* cases are currently supported).
* *
* @since 1.8.0 * @since 1.8.0
* @param integer $number Number to be formatted * @param integer $number Number to be formatted
...@@ -2374,20 +2375,15 @@ function qa_format_number($number, $decimals = 0, $compact = false) ...@@ -2374,20 +2375,15 @@ function qa_format_number($number, $decimals = 0, $compact = false)
$suffix = ''; $suffix = '';
if ($compact && qa_opt('show_compact_numbers')) { if ($compact && qa_opt('show_compact_numbers')) {
$decimals = 0; // when compacting we keep 2-3 significant figures (e.g. 9.1k, 234k)
// only the k/m cases are currently supported (i.e. no billions)
if ($number >= 1000000) { if ($number >= 1000000) {
$number /= 1000000; $number /= 1000000;
$suffix = qa_lang_html('main/_millions_suffix'); $suffix = qa_lang_html('main/_millions_suffix');
$decimals = $number < 100 ? 1 : 0;
} elseif ($number >= 1000) { } elseif ($number >= 1000) {
$number /= 1000; $number /= 1000;
$suffix = qa_lang_html('main/_thousands_suffix'); $suffix = qa_lang_html('main/_thousands_suffix');
} $decimals = $number < 100 ? 1 : 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;
} }
} }
......
...@@ -53,15 +53,15 @@ class AppFormatTest extends PHPUnit_Framework_TestCase ...@@ -53,15 +53,15 @@ class AppFormatTest extends PHPUnit_Framework_TestCase
$qa_phrases_full['main']['_thousands_suffix'] = 'k'; $qa_phrases_full['main']['_thousands_suffix'] = 'k';
$qa_phrases_full['main']['_millions_suffix'] = 'm'; $qa_phrases_full['main']['_millions_suffix'] = 'm';
// $decimal parameter ignored when 'show_compact_numbers' is true $this->assertSame('5', qa_format_number(5.452, 0, 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.5', qa_format_number(5.452, 1, true));
$this->assertSame('5', qa_format_number(5, 1, true)); $this->assertSame('5.0', qa_format_number(5, 1, true));
// $decimal parameter ignored when numbers are compacted
$this->assertSame('9.1k', qa_format_number(9123, 0, true)); $this->assertSame('9.1k', qa_format_number(9123, 0, true));
$this->assertSame('9.1k', qa_format_number(9123, 1, true)); $this->assertSame('9.1k', qa_format_number(9123, 1, true));
$this->assertSame('9k', qa_format_number(9040, 0, true)); $this->assertSame('9.0k', qa_format_number(9040, 0, true));
$this->assertSame('9k', qa_format_number(9040, 1, true)); $this->assertSame('9.0k', qa_format_number(9040, 1, true));
$this->assertSame('9.1k', qa_format_number(9050, 0, true)); $this->assertSame('9.1k', qa_format_number(9050, 0, true));
$this->assertSame('123m', qa_format_number(123456789, 0, true)); $this->assertSame('123m', 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