简体   繁体   English

为什么C#在控制台writeline中显示舍入值,但是作为实际值插入数据库?

[英]Why does C# display a rounded value in console writeline, but inserts into database as actual value?

This is a bit of a brain teaser for me. 这对我来说有点像脑筋急转弯。 We use a remote server to store our data. 我们使用远程服务器来存储我们的数据。 I have no control over naming convention or how data is stored in there. 我无法控制命名约定或数据如何存储在那里。 There is an sproc we have written on that server to call values from a table there. 我们在该服务器上编写了一个用于调用表中值的sproc。 One of the values, price, comes back as 6.52....for this particular example. 其中一个价值,价格,回到6.52 ....这个特定的例子。

在此输入图像描述

The problem was when I then called that value after it was stored in our own local database it showed as 6.51. 问题是当我将它存储在我们自己的本地数据库中之后调用该值时,它显示为6.51。

Thinking it was something to do with rounding, and that it wasn't happening as the data transitioned through my c# program, I pulled a datatable that only retrieved the row in question from the remote server where that value existed(C# method code at end of this message). 认为它与舍入有关,并且当数据通过我的c#程序转换时没有发生,我从一个只存在该值的远程服务器中检索到有问题的行的数据表(C#方法代码结束时)这条消息)。 When I then did a console writeline to show me the value it said it was 6.52. 然后我做了一个控制台写信给我,告诉我它说的价值是6.52。 At this point I was happy because obviously it was pulling the right value and it must be getting changed on our local server as it is entered. 在这一点上,我很高兴,因为显然它正在拉动正确的值,它必须在输入时在我们的本地服务器上进行更改。

在此输入图像描述

At the same time I had changed the number of decimal places for that column in our local server from 2 to 4. When I retrieved that record it showed the value as 6.5199. 与此同时,我将本地服务器中该列的小数位数从2更改为4.当我检索到该记录时,它显示的值为6.5199。 So at least I am getting the right value. 所以至少我得到了正确的价值。 My question is though why would the console writeline be displaying a rounded number? 我的问题是,为什么控制台写信线会显示一个舍入的数字? My assumption would be that it would show the value as it is stored not round it? 我的假设是它会显示价值,因为它存储的不是围绕它?

在此输入图像描述

Does anyone know what is going on here? 有谁知道这里发生了什么?

C# Method: C#方法:

public void getSupplierMasterPriceTEST()
        {
            DataTable priceTable = new DataTable();
            string queryString = "{call sproc164407_2053096_666689 ()}";
            OdbcDataAdapter odbcAdapter = new OdbcDataAdapter();
            OdbcCommand command = new OdbcCommand(queryString, odbcConnection);
            try
            {
                odbcAdapter.SelectCommand = command;
                odbcConnection.Open();
                odbcAdapter.Fill(priceTable);
                Console.WriteLine("Adding to SQL Server");
                //serverConnection.copyTableToServer("Supplier_Master_Price", priceTable);
                foreach(DataRow row in priceTable.Rows)
                {
                    foreach (object item in row.ItemArray)
                    {
                        Console.WriteLine(item);
                    }
                }
                //serverConnection.removeDuplicatesSupplerMasterPrice();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                odbcConnection.Close();
            }
        }

Looks like you are storing as a double and not a decimal. 看起来你要存储为double而不是小数。 Doubles use floating point math which varries in precicion based on the size of the number. 双打使用浮点数学,其根据数字的大小而变化。 Decimals are a number with a defined number of decimal places specified. 小数是一个具有指定的小数位数的数字。

Never ever ever use a double for money calculations. 从来没有使用双倍金钱计算。

Locke beat me to it. 洛克打败了我。 The answer in this question explains it more in depth: Link 这个问题的答案更深入地解释了它: 链接

More information about the rounding error: Link 有关舍入错误的更多信息: 链接

One more inspiring article with a .NET explanation: Binary floating point and .NET 另一篇带有.NET解释的鼓舞人心的文章: 二进制浮点和.NET

尝试将db表中的列类型更改为十进制(18,2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM