Using JavaScript Math.pow for Excel formular
I'm having s开发者_如何学运维ome difficulty getting my JavaScript to produce the same results as a spreadsheet that's been given to me.
The formular is as follows:
=B$2*(1+B$3)^B$6
where
B2 = 40000
B3 = 1%
B6 = 30
The result is 53,914 after it's been rounded.
My JavaScript is as follows:
var B2 = 40000;
var B3 = 1/100;
var B6 = 30;
var result = Math.pow(B2 * (1 + B3),B6);
I always get the result of 1.5539639994483203e+138
Anyone know how I can get my JavaScript to produce the same result as the Excel formular, or where I may have gone wrong with the use of Math.pow function?
It's just your operator precedence that's wrong. Try this:
B2 * Math.pow(1 + B3, B6)
That gives me 53913.95661331625
Excel is evaluating the ^
operator before the *
, so though it looks like (A*B)^C
, it's in fact A*(B^C)
var B2 = 40000;
var B3 = 1/100;
var B6 = 30;
var result = B2*Math.pow(1 + B3,B6);
精彩评论