New course! Every coder should learn Generative AI!
Try a free lesson0
Different result with Excel and C#
Hey people out there, I am currently working on a rather large project in which I have to do some very complex calculations. The formulas are already in an Excel file, so I just have to put them into the code. But I found out, that in my program, I am always up to 2 numbers above or below the result I get when calculated in Excel. Do hou know why I get defferent results? I assume it has something to do with how the programs handle the numbers. I am using doubles in my project. Just in case that matters.
10 Answers
+ 5
Good day Tim Fussnegger! In this case you may consider to use decimal instead of double for more accurate result.
You may find out more information at link below:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types
Hopefully it helps! 😉
+ 6
Tim Fussnegger You're welcome and hope it's working well or otherwise please keep us updated! 😊
+ 5
Tim Fussnegger I'm agree with Fernando Pozzetti, addition information (dataset, Excel & C# formula) will definitely help to troubleshoot the problem.
+ 2
Show your code.
+ 1
Zephyr Koo thank you very much! I will try that.
+ 1
Zephyr Koo I couldn't test it yet. But I will post the result in this thread.
+ 1
Can you add pictures, or the code of both Excel and your program? I am far from being an expert in Excel, but, that way I would be able to check it out.
+ 1
Thank you guys so much for your time and effort! I really appreciate it! I'll put the needed things in as soon as I can. Thanks again!
0
Differences happen because you are not explicitly telling the code to round up, down, or what to do if the result is .5
Always use doubles and tell your code/application to behave the same way by using math functions to round your result, such as Ceiling, Floor, Abs, etc.
0
Zephyr Koo finally I found time to test it. Now, after changing every double to decimal, I end up around 2000 higher than the "right result" in Excel. I have no idea what the reason for that could be.