Skip to content

Commit 70dec1c

Browse files
authored
Merge pull request #7312 from Countly/anna/master
[core] Allow adding multiple digits after comma when formatting.
2 parents 8ec49a7 + ee53199 commit 70dec1c

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

frontend/express/public/javascripts/countly/countly.common.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4559,10 +4559,11 @@
45594559
* @memberof countlyCommon
45604560
* @param {number} second number
45614561
* @param {number} [trimTo=5] number [1,5]
4562+
* @param {number} [digitsAfterComma=1] number of digits after comma for seconds, default is 1, if seconds is less than 0.1 it will be shown as 0
45624563
* @returns {string} return format "Xh Xm Xs", if trimTo is specified the length of the result is trimmed
45634564
* @example trimTo = 2, "Xh Xm Xs" result will be trimmed to "Xh Xm"
45644565
*/
4565-
countlyCommon.formatSecond = function(second, trimTo = 5) {
4566+
countlyCommon.formatSecond = function(second, trimTo = 5, digitsAfterComma = 1) {
45664567
var timeLeft = parseFloat(second);
45674568
var dict = [
45684569
{k: 'year', v: 31536000},
@@ -4571,16 +4572,15 @@
45714572
{k: 'minute', v: 60},
45724573
{k: 'second', v: 1}
45734574
];
4575+
if (digitsAfterComma < 0 || digitsAfterComma > 10) {
4576+
digitsAfterComma = 0;
4577+
}
45744578
var result = {year: 0, day: 0, hour: 0, minute: 0, second: 0};
45754579
var resultStrings = [];
45764580
for (var i = 0; i < dict.length && resultStrings.length < 3; i++) {
45774581
if (dict[i].k === "second") {
4578-
if (timeLeft < 0.1) {
4579-
result.second = 0;
4580-
}
4581-
else {
4582-
result.second = Math.round(timeLeft * 10) / 10;
4583-
}
4582+
4583+
result.second = parseFloat(timeLeft.toFixed(digitsAfterComma));
45844584
}
45854585
else {
45864586
result[dict[i].k] = Math.floor(timeLeft / dict[i].v);

0 commit comments

Comments
 (0)