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
fbc8c141
Commit
fbc8c141
authored
Jan 15, 2015
by
pupi1985
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated qa_format_number function to support compact numbers
parent
17f95d94
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
3 deletions
+29
-3
format.php
qa-include/app/format.php
+27
-3
qa-lang-main.php
qa-include/lang/qa-lang-main.php
+2
-0
No files found.
qa-include/app/format.php
View file @
fbc8c141
...
...
@@ -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 $decimals Amount of decimals to use
* @param bool $compact Whether to show compact numbers or not
* @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
);
}
$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
(
$number
,
$decimals
,
qa_lang_html
(
'main/_decimal_point'
),
qa_lang_html
(
'main/_thousands_separator'
)
);
)
.
$suffix
;
}
...
...
qa-include/lang/qa-lang-main.php
View file @
fbc8c141
...
...
@@ -23,6 +23,8 @@
return
array
(
'_decimal_point'
=>
'.'
,
'_thousands_separator'
=>
','
,
'_thousands_suffix'
=>
'k'
,
'_millions_suffix'
=>
'm'
,
'1_answer'
=>
'1 answer'
,
'1_comment'
=>
'1 comment'
,
'1_day'
=>
'1 day'
,
...
...
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