JavaScript-将3个数字转换为百分比,不需要';t总产量为100%

JavaScript-将3个数字转换为百分比,不需要';t总产量为100%,javascript,math,rounding,percentage,tofixed,Javascript,Math,Rounding,Percentage,Tofixed,我有一个场景,其中我有三个数字: 十七, 十, 九十 我需要将它们转换为整个百分比值(这样,当添加时,总数将达到您预期的100%)。我有这个功能: function roundPercentageTotals(num1, num2, num3) { var total = num1 + num2 + num3; // 117 var num1Total = (num1 / total) * 100; // 14.529914529914531 var num2Tot

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

  • 十七,
  • 十,
  • 九十
  • 我需要将它们转换为整个百分比值(这样,当添加时,总数将达到您预期的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=15
  • 百分比2=9
  • 百分比3=77
  • 加起来是101%,我错在哪里


    提前谢谢你的帮助

    第一次计算得到98%是因为将值向下舍入,第二次计算得到101%是因为将值向上舍入

    将您的
    parseInt()
    更改为
    parseFloat()
    ,使总数接近100%,而不是98%
    parseInt()
    floors小数,它不会对小数进行四舍五入

    关于你的第二次计算,总计101%:把14.5到15,8.5到9四舍五入,你只增加了整整1%。这将使您获得101%而不是100%


    底线是,如果要对精确值进行四舍五入,就不可能始终达到100%的偶数,除非你在整个过程中捏造百分比以适合某个地方。

    你不能将这些数字转换为没有小数的百分比。只有当数字除以100时,它才会起作用。所以这里的答案一定是(1.14.5,2.8.5,3.76.9)。正如你所看到的,有一个“0.1”的百分比丢失了,原因与你抛出的小数相同(即,将14.529914529914531转换为14.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]),它将返回数组中的整个百分比(或者我应该说是最接近的百分比)。

    正是这样做的。为我做了这项工作。 只需传递值并获取始终等于100%的百分比

    const percentRound = require ('percent-round');
    percentRound([16, 56, 18]); // [18, 62, 20]
    

    你唯一做错的就是假设四舍五入后总数必须达到100%。如果你三年前看过这篇文章就好了!哈哈-谢谢你的更新-希望这对其他人也有帮助!这对我来说是有效的,但是当从我所做的最大数中取差值时,
    numar[index]=numar[index]-Math.abs(totalPercentage-100)因为当totalPercentage小于100时,在无限循环中输入