2011-02-16 45 views
0

50, 25, 13计算从非标数的百分比在Javascript

,我想知道如何计算什么25是总量的百分比(这是88 ... 50 + 25 + 13。 ..)。

谢谢!

+0

什么是数据集?字符串,JSON等..? – 2011-02-16 15:11:28

+3

`var percent =(25/88)* 100;`? – 2011-02-16 15:11:31

回答

3
function CalculatePercent(value, total) 
{ 
    return 100 * (value/total); 
} 

var x = CalculatePercent(25, 88); 
1

添加它们放在一起乘,除运算在伪代码为您提供:

percentage = element_value/sum(array) * 100 

element_value将你的情况25。

0
//input array is the data set to use for the calculation 
//indexNum is the array location (index) to use to figure out the percentage for 
function calculatePercentage(inputArray, indexNum) { 
    var sum=0; 
    for(var i=0, len=inputArray.length; i<len; i++) { 
    sum = sum + inputArray[i]; 
    } 
    return (inputArray[indexNum]/sum) * 100; 
} 

//example usage 
var dataSet = [50, 25, 13]; 
//find percentage that 25 is 
$percentage = calculatePercentage(dataSet, 1);