views:

23

answers:

2

Hey all i am trying to figure out how to go about calculating this formula from an Excel 2007 fx to javascript:

 =(B12*'ROI endo only'!B5)/(1-(1+Data!B12)^(-Data!B13))

 WHERE
   B12 = 0.49%
   B5  = 99,500
   B13 = 48

So far i have this as my javascript code:

 var theCalc = (0.49 * 99500) / (1 - (1 + 0.49)^(-48));

 alert(theCalc);

Though, i am not getting the correct result as what i get on the spreadsheet. The spreadsheet has a value of 2,332 and i am getting -1015.72916... from the javascript code.

Any help would be great in solving my problem :o)

UPDATE

 var theCalc = (0.0049 * 99500) / (1 - Math.pow((1 + 0.0049), -48));

David

A: 

I believe the problem is with the ^. In javascript, ^ does not mean exponent like it does in VB. You have to use the Math.pow method instead. Try this JavaScript:

var theCalc = (0.49 * 99500) / Math.pow( 1 - (1 + 0.49), -48 );
David
Looking at his original code, I would think the divisor should be (1 - Math.pow((1+.49), -48)
Tommy
David: i get 6.567967606278839e-11 when doing that...
StealthRT
Tommy: i get 48755.00023718012 when using your code...
StealthRT
added a comment to your question and an answer below. I think it was a combination of what David mentioned, using Math.pow, having your divisor in the correct order and using .0049 instead of .49
Tommy
A: 
<html>
<head>
<title>test</title>
</head>

<body onload="testfun();">

<script type="text/javascript"> 
function testfun(){
    document.getElementById('testRes').innerHTML = (.0049 * 99500) / (1 - Math.pow((1+.0049), -48));
}
</script>

The answer:
<div id="testRes"></div>

This gives a result of 2331.290084493742

Tommy
Changing the .49 to 0.0049 did the trick with your code, Tommy but how do i round it since the answer is 2331.290084493742 and it should be 2,332?
StealthRT
I think we are seeing a little floating point math / rounding error between Excel and JS. For example, changing my code to this: ((.0049 * 99500).toFixed(4) / (1 - Math.pow((1+.0049), -48)).toFixed(4)).toFixed(0), gives a result of 2,332. Basically, using the toFixed properties to limit the number of decimals coming out of each part of the function, then using .toFixed(0) to return the whole number. I think you're on the right track, just need to see how much precision you need in these numbers.
Tommy
Ah, thanks Tommy. That did it :o)
StealthRT