The same multiplication returns a different result after using Excel OLEDB in .NET.

I have the following C # code:

static void Main(string[] args) { double a = 1.2d; double b = 3.1d; double c = 0.17241379310344829; double d = 0.25d; Console.WriteLine("{0:G17}", a * b * c * d); string CONNECTION_STRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=\"Excel 12.0 Xml;HDR=YES\";"; using(var conn = new OleDbConnection(string.Format(CONNECTION_STRING, @"C:\TEMP\anyExcelFile.xlsx"))) { conn.Open(); using(var sqlCmd = conn.CreateCommand()) { sqlCmd.CommandType = System.Data.CommandType.Text; sqlCmd.CommandText = "INSERT INTO [Sheet1$A1:A] VALUES (1)"; sqlCmd.ExecuteNonQuery(); } } Console.WriteLine("{0:G17}", a * b * c * d); } 

When I launched it, Console.WriteLine gives me the following results:

 0.16034482758620688 0.16034482758620691 

You need to have an excel file in C: \ TEMP \ anyExcelFile.xlsx (with a sheet named Sheet1) in order to be able to run it.

If I comment out the line ExecuteNonQuery and run it again, I get:

 0.16034482758620688 0.16034482758620688 

Why does excel lead to a double operation to return different results?

+5
source share

All Articles