I'm having some 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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top