2016-08-04 3 views
1

合計収率100%ありませんそれらをパーセント値全体に変換する必要があります(追加すると、期待どおりに合計100%になります)。私はこの機能を持っています:JavaScriptはパーセンテージに3つの数字を変換すること、私は3つの数字を持っているシナリオを有する

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. コマンドpercentage1 = 15
  2. percentage2に= 9
  3. Percentage3 = 77 101%まで追加

、どこが間違っているんですか?

事前にお問い合わせいただきありがとうございます。あなたがダウン値を丸め、そしてあなたがまで彼らにを丸めているので、次に2番目に101%を取得しているので、

+1

あなたが間違っているのは、四捨五入後に合計が100%になる必要があることです。 – recursive

答えて

1

これは数学的に見えるので、私が探していたものを正確に達成することはできません。しかし、私は最終的に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([13,54,38])として、roundPercentageTotalsに数字の配列を渡すと、それが返されますパーセンテージ(または私が言うべき最も近いパーセンテージ)は配列内の数字です。

6

あなたは最初の計算では98%を取得しています。

parseInt()parseFloat()に変更すると、合計が98%ではなく100%に近づくようになります。 parseInt()階の小数点以下は四捨五入されません。

2番目の計算に関しては、合計101%:14.5-15と8.5-9を切り上げると、完全に1%が追加されました。これにより、100%ではなく101%が残されます。

最後の行は、途中のどこかにパーセンテージを振りかざしていない限り、正確な値を丸めようとすると、一貫して100%を達成できないということです。

1

小数点なしでは、これらの数値をパーセントで変換することはできません。数字が100で割られている場合にのみ機能します。ここでの回答は(1.14.5,2.8.53,7.6.9)でなければなりません。あなたが見たように、あなたが投げた小数点の同じ理由で(つまり、14.529914529914531を14.5に変換することによって)「0.1」パーセントが失われています。

関連する問題