3

我有一个场景,我有三个数字:

  1. 17
  2. 10
  3. 90

我需要将它们转换为完整的百分比值(这样添加时,总计 100%,如您所料)。我有这个功能:

function roundPercentageTotals(num1, num2, num3) {
    var total = num1 + num2 + num3;  // 117

    var num1Total = (num1 / total) * 100;  // 14.529914529914531
    var num2Total = (num2 / total) * 100;  //  8.547008547008546
    var num3Total = (num3 / total) * 100;  // 76.92307692307693

    var num1ToDecimal = num1Total.toFixed(1); // 14.5
    var num2ToDecimal = num2Total.toFixed(1); //  8.5
    var num3ToDecimal = num3Total.toFixed(1); // 76.9

    var totalPercentage = parseInt(num1ToDecimal) + parseInt(num2ToDecimal) + parseInt(num3ToDecimal); // 98

    return { percentage1: Math.round(num1ToDecimal, percentage2: Math.round(num2ToDecimal), percentage3: Math.round(num3ToDecimal) };
}

在我的示例中,计算的总百分比为 98%。其次是:

  1. 百分比 1 = 15
  2. 百分比2 = 9
  3. 百分比3 = 77

加起来是 101%,我哪里错了?

提前感谢您的帮助!

4

4 回答 4

6

您在第一次计算中得到 98% 是因为您将值向下舍入,然后在第二次计算中得到 101% 是因为您将它们向上舍入。

将您的更改parseInt()parseFloat()使您的总数接近 100% 而不是 98%。parseInt()楼层小数,它不会四舍五入。

关于您的第二次计算总计 101%:通过将 14.5 舍入到 15 和 8.5 到 9,您刚刚添加了整整 1%。这给你留下了 101% 而不是 100%。

底线是,如果您要对精确值进行四舍五入,您将无法始终达到 100%,除非您捏造百分比以适应沿途的某个地方。

于 2016-08-04T14:25:52.460 回答
5

好的,所以从数学上看,我无法完全实现我想要的。但是,我需要对数字进行四舍五入,使其最终等于 100%(所有这些数字都是四舍五入的,因此并不完全准确)。

这是我的解决方案,以防万一这对其他人有用:

function roundPercentageTotals(numArr) {

    // Total of all numbers passed.
    var total = numArr[0] + numArr[1] + numArr[2];

    // Percentage representations of each number (out of 100).
    var num1Percent = Math.round((numArr[0] / total) * 100);
    var num2Percent = Math.round((numArr[1] / total) * 100);
    var num3Percent = Math.round((numArr[2] / total) * 100);

    // Total percent of the 3 numbers combined (doesnt always equal 100%).
    var totalPercentage = num1Percent + num2Percent + num3Percent;

    // If not 100%, then we need to work around it by subtracting from the largest number (not as accurate but works out).
    if (totalPercentage != 100) {
        // Get the index of the largest number in the array.
        var index = getLargestNumInArrayIndex(numArr);

        // Take the difference away from the largest number.
        numArr[index] = numArr[index] - (totalPercentage - 100);

        // Re-run this method recursively, until we get a total percentage of 100%.
        return roundPercentageTotals(numArr);
    }

    // Return the percentage version of the array passed in.
    return [num1Percent, num2Percent, num3Percent];
}

function getLargestNumInArrayIndex(array) {
    return array.indexOf(Math.max.apply(Math, array));
}

将数字数组传递给roundPercentageTotals,例如roundPercentageTotals([13,54,38]),它将返回数组中的整个百分比(或我应该说的最接近的百分比)数字。

于 2016-08-18T09:34:45.863 回答
3

percent-round就是这样做的。为我完成了这项工作。只需传递值并返回总加起来为 100% 的百分比。

const percentRound = require ('percent-round');
percentRound([16, 56, 18]); // [18, 62, 20]
于 2019-09-07T12:10:28.413 回答
1

您无法将这些数字转换为没有小数的百分比。只有当数字除以 100 时才会起作用。所以这里的答案必须是 (1. 14.5 , 2. 8.5 , 3. 76.9)。正如您所看到的,由于与您抛出的小数相同的原因(即通过将 14.529914529914531 转换为 14.5),缺少“0.1”%。

于 2016-08-04T14:36:12.200 回答