Sukh
Sukh

Reputation: 25

Using JavaScript Math.pow for Excel formular

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?

Upvotes: 1

Views: 2730

Answers (2)

Andrew D.
Andrew D.

Reputation: 8220

var B2 = 40000; 
var B3 =  1/100; 
var B6 = 30;

var result = B2*Math.pow(1 + B3,B6);

Upvotes: 0

Flambino
Flambino

Reputation: 18773

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)

Upvotes: 2

Related Questions