diff --git a/server/services/torrentService.js b/server/services/torrentService.js index 3e6ac7cf..0f4361de 100644 --- a/server/services/torrentService.js +++ b/server/services/torrentService.js @@ -5,6 +5,7 @@ const config = require('../../config'); const formatUtil = require('../../shared/util/formatUtil'); const methodCallUtil = require('../util/methodCallUtil'); const serverEventTypes = require('../../shared/constants/serverEventTypes'); +const truncateTo = require('../util/numberUtils'); const torrentListPropMap = require('../constants/torrentListPropMap'); const torrentServiceEvents = require('../constants/torrentServiceEvents'); const torrentStatusMap = require('../../shared/constants/torrentStatusMap'); @@ -116,9 +117,9 @@ class TorrentService extends BaseService { const percentComplete = (torrentDetails.bytesDone / torrentDetails.sizeBytes) * 100; if (percentComplete > 0 && percentComplete < 10) { - return Number(percentComplete.toFixed(2)); + return Number(truncateTo(percentComplete, 2)); } else if (percentComplete > 10 && percentComplete < 100) { - return Number(percentComplete.toFixed(1)); + return Number(truncateTo(percentComplete, 1)); } return percentComplete; diff --git a/server/util/clientResponseUtil.js b/server/util/clientResponseUtil.js index 833e9feb..fe645a05 100644 --- a/server/util/clientResponseUtil.js +++ b/server/util/clientResponseUtil.js @@ -1,4 +1,5 @@ const geoip = require('geoip-lite'); +const truncateTo = require('./numberUtils'); const torrentFilePropsMap = require('../../shared/constants/torrentFilePropsMap'); const torrentPeerPropsMap = require('../../shared/constants/torrentPeerPropsMap'); const torrentTrackerPropsMap = require('../../shared/constants/torrentTrackerPropsMap'); @@ -72,7 +73,7 @@ let clientResponseUtil = { processFile(file) { file.filename = file.pathComponents[file.pathComponents.length - 1]; - file.percentComplete = ((file.completedChunks / file.sizeChunks) * 100).toFixed(0); + file.percentComplete = truncateTo((file.completedChunks / file.sizeChunks) * 100); delete file.completedChunks; delete file.pathComponents; diff --git a/server/util/numberUtils.js b/server/util/numberUtils.js new file mode 100644 index 00000000..f47d3230 --- /dev/null +++ b/server/util/numberUtils.js @@ -0,0 +1,6 @@ +const truncateTo = (num, precision = 0) => { + const factor = 10 ** precision; + return Math.floor(num * factor) / factor; +}; + +module.exports = truncateTo;