From 086f935d5946dd368d444d6f035322176b9ca325 Mon Sep 17 00:00:00 2001 From: Jonathan Putney Date: Thu, 26 Dec 2019 14:14:07 -0500 Subject: [PATCH] Checking if more than 2 decimal places, before using toFixed --- src/utilities.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/utilities.js b/src/utilities.js index 1e651c2..4e5f19a 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -28,7 +28,10 @@ export function getSecondsAsHHMMSS(totalSeconds: Number) { const dateObj = new Date(totalSeconds * 1000); const minutes = dateObj.getUTCMinutes(); // make sure we add any possible decimal value - const seconds = (dateObj.getSeconds() + (totalSeconds % 1.0)).toFixed(2); + let seconds = (dateObj.getSeconds() + (totalSeconds % 1.0)); + if (countDecimals(seconds) > 2) { + seconds = seconds.toFixed(2); + } return hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' + @@ -119,8 +122,7 @@ export function getDurationAsSeconds(duration: String, durationRegex: RegExp) { const milliseconds = Number(Number(seconds) % 1).toFixed(6) * 1000.0; anchor.setMilliseconds(anchor.getMilliseconds() + milliseconds); } - - return (((anchor * 1.0) - now) / 1000.0).toFixed(2); + return ((anchor * 1.0) - now) / 1000.0; } /** @@ -220,3 +222,13 @@ export function unflatten(data) { } return result[''] || result; } + +/** + * Counts the number of decimal places + * @param {number} num + * @return {number} + */ +export function countDecimals(num: number) { + if (Math.floor(num) === num) return 0; + return num.toString().split('.')[1].length || 0; +}