J09
J09

Reputation: 169

Excel formula into C# implementation

So my goal is to implement an Excel formula in C#, but I am unable to produce exact results.

The Excel formula:

=(V11-AH11)*(-1+(1+M12)^((I12-J12)/365)) where  

V11 = 500000  
AH11 = 10000  
M12 = 0.41%  
I12 = 3/1/2016 (mm/dd/yyyy)  
J12 = 2/1/2016 (mm/dd/yyyy)  

Result = 159.96

my C# implementation:

result = Convert.ToDouble((V11 - AH11) * (-1 + Math.Pow((1 + M12), (((Convert.ToDateTime(I12) - Convert.ToDateTime(J12)).TotalDays / 365)))));  

Result : 159.32

What am I doing wrong? Any help is highly appreciated.

Thanks. :)

Upvotes: 1

Views: 264

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

The problem here must be with accuracy.

Even Wolfram Alpha agrees with C#, the result is closer to 159.32 than it is to 159.96.

My bet would be the percentage, which looks calculated, all the other numbers are nice whole integer like numbers.

I asked Wolfram Alpha to solve for the percentage in order to get the result 159.96 and it came back with 0.411653

So the C# code is the most correct, your Excel assumptions are at fault, you have more accuracy than you're displaying.

Upvotes: 1

Related Questions