diff --git a/src/libs/routes/classroomCount.js b/src/libs/routes/classroomCount.js index c258ba03f471d0e8b4d49b98a30dd4145ba76b48..27ff6e86e05215d48bb327beb3b536179000faf2 100644 --- a/src/libs/routes/classroomCount.js +++ b/src/libs/routes/classroomCount.js @@ -1023,19 +1023,24 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => { for (let value of teacherByFormation) { sum += value; } + teacherByFormation[1] += teacherByFormation[0] + teacherByFormation[0] = 0 + let diff = 1 - sum; - // Se for menor/maior que 100 soma/subtrai na P2 + + // Se soma de porcentagens for menor/maior que 100, faz correção if (Math.abs(diff) > 0.0001) { - teacherByFormation[1] += diff; + // Garante que a porcentagem corrigida não ficará negativa + let indDiff = 1; + while (teacherByFormation[indDiff] + diff < 0) indDiff++; + teacherByFormation[indDiff] += diff; } - teacherByFormation[1] += teacherByFormation[0] - teacherByFormation[0] = 0 // Cria vetor de porcentagens de carreira dos professores req.teacherFormation.forEach((formation, i) => { lastCity.percentage_teacher_career.push({ formation_level_id: formation.idFormationLevel, - percentage: (teacherByFormation[i]*100).toFixed(2), + percentage: Number((teacherByFormation[i]*100).toFixed(2)), }) });