Different result with Excel and C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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.

22nd Jan 2020, 10:53 PM
ExampleWasTaken
ExampleWasTaken - avatar
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! 😉
23rd Jan 2020, 12:37 AM
Zephyr Koo
Zephyr Koo - avatar
+ 6
Tim Fussnegger You're welcome and hope it's working well or otherwise please keep us updated! 😊
24th Jan 2020, 7:33 PM
Zephyr Koo
Zephyr Koo - avatar
+ 5
Tim Fussnegger I'm agree with Fernando Pozzetti, addition information (dataset, Excel & C# formula) will definitely help to troubleshoot the problem.
30th Jan 2020, 12:20 AM
Zephyr Koo
Zephyr Koo - avatar
+ 2
Show your code.
23rd Jan 2020, 2:32 AM
Maninder $ingh
Maninder $ingh - avatar
+ 1
Zephyr Koo thank you very much! I will try that.
23rd Jan 2020, 5:30 AM
ExampleWasTaken
ExampleWasTaken - avatar
+ 1
Zephyr Koo I couldn't test it yet. But I will post the result in this thread.
24th Jan 2020, 7:50 PM
ExampleWasTaken
ExampleWasTaken - avatar
+ 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.
29th Jan 2020, 7:35 PM
Fernando Pozzetti
Fernando Pozzetti - avatar
+ 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!
30th Jan 2020, 12:27 AM
ExampleWasTaken
ExampleWasTaken - avatar
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.
23rd Jan 2020, 3:16 AM
Fernando Pozzetti
Fernando Pozzetti - avatar
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.
29th Jan 2020, 3:07 PM
ExampleWasTaken
ExampleWasTaken - avatar