Problem With Precision

V

Victor

When I loop through a datatable rows and add Amt field content (which
is float in database) sometimes it adds upto High Precision value
leaving a difference of -1.81898940354586E-12 though I am only adding
Currency Format numbers i.e., 23.43,56.99 etc,So I am not able to
directly compare the total(Computer value) to a value in text field.







Here is the Code
If Session("DT") Is Nothing Then
Dim SQL As String = "SELECT AcctNum, Amount FROM
Accts where 1=2"
Dim Dbase As New DBInterface(DBConnString)
Session("DT") = Dbase.GetDataTable(SQL)
End If
Dim DT As DataTable
DT = Session("DT")
Dim Drow As DataRow = DT.NewRow
Drow("AcctNum") = txtAcct.Text
Drow("Amount") = txtAmt.Text
DT.Rows.Add(Drow)
Dim Total As Double
Dim EachRow As DataRow
For Each EachRow In DT.Rows
Total += EachRow("Amount")
Next

dgrd.Columns.Item(0).FooterText = "Total"
dgrd.Columns.Item(1).FooterText = Total '.ToString("c")
txtTotal.Text = Total
Response.Write(Total - CDbl(txtTotal.Text))
Session("DT") = DT
dgrd.DataSource = DT
dgrd.DataBind()
 
C

Chris Dunaway

Instead of using Double, try using Decimal instead. Your problem
appears because a floating point number cannot accurately be
represented in binary. There have been numerous posts on these
newsgroups which explains it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top