Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
08963093
Commit
08963093
authored
May 17, 2020
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve compact numbers
Fixes #781
parent
0f7e1461
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
14 deletions
+10
-14
format.php
qa-include/app/format.php
+5
-9
AppFormatTest.php
qa-tests/AppFormatTest.php
+5
-5
No files found.
qa-include/app/format.php
View file @
08963093
...
...
@@ -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.
* 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
* @param integer $number Number to be formatted
...
...
@@ -2374,20 +2375,15 @@ function qa_format_number($number, $decimals = 0, $compact = false)
$suffix
=
''
;
if
(
$compact
&&
qa_opt
(
'show_compact_numbers'
))
{
$decimals
=
0
;
// only the k/m cases are currently supported (i.e. no billions)
// when compacting we keep 2-3 significant figures (e.g. 9.1k, 234k)
if
(
$number
>=
1000000
)
{
$number
/=
1000000
;
$suffix
=
qa_lang_html
(
'main/_millions_suffix'
);
$decimals
=
$number
<
100
?
1
:
0
;
}
elseif
(
$number
>=
1000
)
{
$number
/=
1000
;
$suffix
=
qa_lang_html
(
'main/_thousands_suffix'
);
}
// 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
;
$decimals
=
$number
<
100
?
1
:
0
;
}
}
...
...
qa-tests/AppFormatTest.php
View file @
08963093
...
...
@@ -53,15 +53,15 @@ class AppFormatTest extends PHPUnit_Framework_TestCase
$qa_phrases_full
[
'main'
][
'_thousands_suffix'
]
=
'k'
;
$qa_phrases_full
[
'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'
,
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
(
'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
,
1
,
true
));
$this
->
assertSame
(
'9k'
,
qa_format_number
(
9040
,
0
,
true
));
$this
->
assertSame
(
'9k'
,
qa_format_number
(
9040
,
1
,
true
));
$this
->
assertSame
(
'9
.0
k'
,
qa_format_number
(
9040
,
0
,
true
));
$this
->
assertSame
(
'9
.0
k'
,
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
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment