开发者

JavaScript math calculation

开发者 https://www.devze.com 2023-04-10 20:06 出处:网络
I need to perform a math calculation in JavaScript that I am unsure how to do so. The for开发者_StackOverflowmula is as follows:

I need to perform a math calculation in JavaScript that I am unsure how to do so.

The for开发者_StackOverflowmula is as follows:

result = (scale * weighting) / 2

where the result I will need to two decimal places.

Example formula that I need to do in JavaScript might be:

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

I am also unsure if I have to also convert this to int.


toFixed:

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

Or if you need an integer, parseInt:

parseInt(result, 10);


Not entirely sure what you want, but this might help:

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

http://jsfiddle.net/kQpma/


In JavaScript everything number is a floating point number. No explicit type conversion needed.

ref : http://oreilly.com/javascript/excerpts/learning-javascript/javascript-datatypes-variables.html ( search for "The Number Data Type" )

0

精彩评论

暂无评论...
验证码 换一张
取 消