diff --git a/.circleci/config.yml b/.circleci/config.yml index b2b4a25a..5e41ee80 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -83,7 +83,7 @@ workflows: branches: only: - dev - - fix/search + - fix/floating-point-arhithmetic - "build-qa": context: org-global diff --git a/package.json b/package.json index 7bb22c6c..c6cdb098 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "body-parser": "^1.15.1", "config": "^3.0.1", "cors": "^2.7.1", + "decimal.js": "^10.4.3", "deep-equal": "^2.2.0", "dotenv": "^8.2.0", "elasticsearch": "^16.7.3", diff --git a/src/common/challenge-helper.js b/src/common/challenge-helper.js index ce13b8f3..a7372149 100644 --- a/src/common/challenge-helper.js +++ b/src/common/challenge-helper.js @@ -8,6 +8,7 @@ const config = require("config"); const helper = require("./helper"); const constants = require("../../app-constants"); const axios = require("axios"); +const Decimal = require("decimal.js"); const { getM2MToken } = require("./m2m-helper"); const { hasAdminRole } = require("./role-helper"); const { ensureAcessibilityToModifiedGroups } = require("./group-helper"); @@ -350,7 +351,7 @@ class ChallengeHelper { convertPrizeSetValuesToCents(prizeSets) { prizeSets.forEach((prizeSet) => { prizeSet.prizes.forEach((prize) => { - prize.amountInCents = prize.value * 100; + prize.amountInCents = new Decimal(prize.value).mul(100).toNumber(); delete prize.value; }); }); @@ -360,13 +361,17 @@ class ChallengeHelper { prizeSets.forEach((prizeSet) => { prizeSet.prizes.forEach((prize) => { if (prize.amountInCents != null) { - prize.value = prize.amountInCents / 100; + prize.value = parseFloat(new Decimal(prize.amountInCents).div(100).toFixed(2)); + delete prize.amountInCents; } }); }); if (overview && !_.isUndefined(overview.totalPrizesInCents)) { - overview.totalPrizes = overview.totalPrizesInCents / 100; + overview.totalPrizes = parseFloat( + new Decimal(overview.totalPrizesInCents).div(100).toFixed(2) + ); + delete overview.totalPrizesInCents; } } diff --git a/yarn.lock b/yarn.lock index 7d7fd30d..c2a451ca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1058,6 +1058,11 @@ decamelize@^1.2.0: resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" integrity sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA== +decimal.js@^10.4.3: + version "10.4.3" + resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.4.3.tgz#1044092884d245d1b7f65725fa4ad4c6f781cc23" + integrity sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA== + deep-eql@^4.1.2: version "4.1.3" resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-4.1.3.tgz#7c7775513092f7df98d8df9996dd085eb668cc6d"