Skip to content

Commit cd525b2

Browse files
authored
Merge pull request #622 from topcoder-platform/fix/floating-point-arhithmetic
fix: winner prize mismatch (plat-2732)
2 parents d195730 + 84f4c00 commit cd525b2

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ workflows:
8383
branches:
8484
only:
8585
- dev
86-
- fix/search
86+
- fix/floating-point-arhithmetic
8787

8888
- "build-qa":
8989
context: org-global

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"body-parser": "^1.15.1",
5252
"config": "^3.0.1",
5353
"cors": "^2.7.1",
54+
"decimal.js": "^10.4.3",
5455
"deep-equal": "^2.2.0",
5556
"dotenv": "^8.2.0",
5657
"elasticsearch": "^16.7.3",

src/common/challenge-helper.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const config = require("config");
88
const helper = require("./helper");
99
const constants = require("../../app-constants");
1010
const axios = require("axios");
11+
const Decimal = require("decimal.js");
1112
const { getM2MToken } = require("./m2m-helper");
1213
const { hasAdminRole } = require("./role-helper");
1314
const { ensureAcessibilityToModifiedGroups } = require("./group-helper");
@@ -350,7 +351,7 @@ class ChallengeHelper {
350351
convertPrizeSetValuesToCents(prizeSets) {
351352
prizeSets.forEach((prizeSet) => {
352353
prizeSet.prizes.forEach((prize) => {
353-
prize.amountInCents = prize.value * 100;
354+
prize.amountInCents = new Decimal(prize.value).mul(100).toNumber();
354355
delete prize.value;
355356
});
356357
});
@@ -360,13 +361,17 @@ class ChallengeHelper {
360361
prizeSets.forEach((prizeSet) => {
361362
prizeSet.prizes.forEach((prize) => {
362363
if (prize.amountInCents != null) {
363-
prize.value = prize.amountInCents / 100;
364+
prize.value = parseFloat(new Decimal(prize.amountInCents).div(100).toFixed(2));
365+
364366
delete prize.amountInCents;
365367
}
366368
});
367369
});
368370
if (overview && !_.isUndefined(overview.totalPrizesInCents)) {
369-
overview.totalPrizes = overview.totalPrizesInCents / 100;
371+
overview.totalPrizes = parseFloat(
372+
new Decimal(overview.totalPrizesInCents).div(100).toFixed(2)
373+
);
374+
370375
delete overview.totalPrizesInCents;
371376
}
372377
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,11 @@ decamelize@^1.2.0:
10581058
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
10591059
integrity sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==
10601060

1061+
decimal.js@^10.4.3:
1062+
version "10.4.3"
1063+
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.4.3.tgz#1044092884d245d1b7f65725fa4ad4c6f781cc23"
1064+
integrity sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==
1065+
10611066
deep-eql@^4.1.2:
10621067
version "4.1.3"
10631068
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-4.1.3.tgz#7c7775513092f7df98d8df9996dd085eb668cc6d"

0 commit comments

Comments
 (0)