2011-10-04 123 views
0

希望有人可以协助,但我需要在javascript中执行数学计算,我不确定该怎么做。Javascript数学计算

的计算公式如下:

result = (scale * weighting)/2 

其中结果我需要到小数点后两位。

例公式,我需要在JavaScript中做的可能是:

result = ((3 * 2) + (2 * 1.5))/2 

我也不能确定,如果我必须也将此转换为int

会很感激在这一些指导。

谢谢。

+0

为什么不'结果=(比例*加权)/ 2.0'? vietean

回答

6

toFixed

function algorithm(scale, weighting) { 
    var result = (scale * weighting)/2; 
    return result.toFixed(2); 
} 

或者如果你需要一个整数,parseInt

parseInt(result, 10); 
0

不完全知道你想要什么,但是这可能帮助:

result = Math.round(((scale * weighting)/2) * 100)/100; 

http://jsfiddle.net/kQpma/