Vb.net舍入不和谐

时间:2014-08-18 10:18:21

标签: sql sql-server vb.net rounding

我正在使用vb.net使用以下代码汇总数据表中的一些信息。

出于某种原因,除了1之外,舍入适用于所有值。

代码:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    If TextBox1.Text = "" Then
        'Do Nothing
    Else

        Dim conn As New SqlClient.SqlConnection
        With conn
            .ConnectionString = "server=inlt01\SQLEXPRESS; database=OrbisBilling; integrated security=yes"
            .Open()
        End With

        Dim cmd As New SqlClient.SqlCommand
        With cmd
            .CommandText = "SELECT [Account], [Customer Lookup],ROUND (SUM ([ChargedAmount]),2) as [Sum of Buy Price],ROUND (SUM ([Sell Price]),2) as [Sum of Sell Price],[Tariff Lookup] FROM [OrbisBilling].[dbo].[" + TextBox1.Text + "] GROUP BY [Account], [Customer Lookup],[Tariff Lookup] Order by [Customer Lookup]"
            .CommandType = CommandType.Text
            .CommandTimeout = 30
            .Connection = conn
        End With
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader)

        With OrbisViewBillForm.DataGridView1
            .AutoGenerateColumns = True
            .DataSource = dt
        End With

        cmd.Dispose()
        cmd = Nothing
        conn.Dispose()
        conn = Nothing


        OrbisViewBillForm.Show()

    End If
End Sub

正如您所见,我将“卖出价”的总和减去2位小数。

结果如下。

56.1
1.5899999999999999
133.64
12.67
14.93
0.76
0.57
4.25
2.24
0.06
7.75
4.31
7.96
0.78
0.01
33.55

如果四舍五入为2,为什么我会得到1.5899999999999999?

为了帮助解决问题,我将舍入更改为3,并获得以下不一致。

56.1
1.5899999999999999
133.643
12.671
14.928
0.764
0.568
4.253
2.2439999999999998
0.058
7.745
4.306
7.9559999999999995
0.779
0.005
33.554

非常感谢任何帮助。

谢谢,

1 个答案:

答案 0 :(得分:3)

SQL Server FLOATREAL数据类型不会精确存储十进制值。

您应该考虑将数据库列更改为DECIMAL或NUMERIC。