.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Error in Addition of Double Variables
 

Error in Addition of Double Variables

Hi,

This is Aravind. I got an error due to addition of two varibles which is declared as "Double". It shows the wrong sum up.

Sample Code is as below:

Dim amt1, amt2, z As Double

z = 0.0

amt1 = 74.68

amt2 = 168.49

z = amt1 + amt2

The Answer should be 243.17

But ...It shows

"243.17000000000002".....

Why it happens ....it will leads to Big problem....when summing up the Money values....

Can u please clarify this....

Its urgent....

Regards

Aravindkumar.P

AravindKumar

You can't always express the exact value using floating point data types; see http://docs.sun.com/source/806-3568/ncg_goldberg.htmlfor a more in-depth discussion. Please note that this is *not* a .NET framework issue, it is simply a fact of life when it comes to how computers in general represent floating point values.

I would recommend using System.Decimal for financial calculations.

Best regards,
Johan Stenberg

MSFT Johan Stenberg

You can't always express the exact value using floating point data types; see http://docs.sun.com/source/806-3568/ncg_goldberg.htmlfor a more in-depth discussion. Please note that this is *not* a .NET framework issue, it is simply a fact of life when it comes to how computers in general represent floating point values.

I would recommend using System.Decimal for financial calculations.

Best regards,
Johan Stenberg

MSFT Johan Stenberg
Classic floating point accuracy issue. Do *not* use the 'double' type to store currency amounts, use System.Decimal.
nobugz

Hi Mr.Stenberg,

Thanks for your reply.

I know that there is a solution by using System.Decimal or Rounding up the Value by 2 decimal.

But i need the answer, why it shows the wrong sum up...eventhough the Rounding error has occured in Floats.

Why u preferred double. We are using ost of the cases in double for Financial Problems...It leads to the same Error....

plsclarify the Sum up error....This is not an system Error......Its will show the correct sum up....Sql Server...as same as in float

why it so.........

Its urgent..we are using VB.Net......we already gave the set up to our clients..

this leads to criticla issue.....why its happend in .Net not in Sql Server 2003...

Please Calrify..

Regards

Aravindkumar.P

AravindKumar

Thanks for your reply.

I know that there is a solution by using System.Decimal or Rounding up the Value by 2 decimal.

But i need the answer, why it shows the wrong sum up...eventhough the Rounding error has occured in Floats.

Why u preferred double. We are using ost of the cases in double for Financial Problems...It leads to the same Error....

plsclarify the Sum up error....This is not an system Error......Its will show the correct sum up....Sql Server...as same as in float

why it so.........

Its urgent..we are using VB.Net......we already gave the set up to our clients..

this leads to criticla issue.....why its happend in .Net not in Sql Server 2003...

Please Calrify..

Regards

Aravindkumar.P

AravindKumar
You might be able to work around this problem by properly formatting the computation result when you display or print it. Use something like this:

Dim amt1 As Double = 74.68
Dim amt2 As Double = 168.49
Dim sum As Double = amt1 + amt2
Label1.Text = String.Format("{0:N2}", sum)

String.Format("{0:C}", sum) works too, that puts the currency symbol ('$' where I live) in front of the number.

nobugz

Aravindkumar,

I assume that you mean SQL server 2005 and not SQL Server 2003... What datatype are you using on SQL Server? If you are using the equivalent of VB's Double datatype (which happens to be float) you should (to the best of my knowledge - I'm not an SQL Server expert) get the same result.

How did you determine that SQL Server 2005 gave you a different result?

Best regards,
Johan Stenberg

MSFT Johan Stenberg

Hi

For Your Information:

Ths following results for different data types...

AS u know very well There is no double data typein SQL server....instead of that real.Float.. we can use....

select 0.68+ .49


declare @a as float,
@a1 as float
set @a = 0.68
set @a1 = 0.49
select @a+@a1
--Output = 1.1699999999999999


declare @a as real,
@a1 as real
set @a = 0.68
set @a1 = 0.49
select @a+@a1
--Output : 1.1700001

declare @a as decimal,
@a1 as decimal
set @a = 0.68
set @a1 = 0.49
select @a+@a1
--Output : 1

declare @a as money,
@a1 as money
set @a = 0.68
set @a1 = 0.49
select @a+@a1
---Output : 1.1700

why there is a lot of sum up difference in Real/float between Sql and .net for the same category of data Excet Money Column in SQl / Decima in ,Net.

Please Advice Me.

thanks

Aravindkumar.P

AravindKumar
SQL's 'real' data type is the same as .NET's 'Single' type. SQL's 'float' matches .NET's 'Double'. 'decimal' and 'money' roughly match 'Decimal'.

SQL uses the same floating point hardware as .NET so has the same accuracy issues.
nobugz

You can use google to search for other answers

Custom Search

More Threads

• System Diagnostic process - ends prematurly . Able to see the process started but no result.
• Can't iterate through all subfolders of a directory? Say it ain't so!
• Generic's - Collections
• Connection problim with database(A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll)
• What happens when you call RaiseEvent?
• dll settings
• Common BCL and Interfaces *.cd files
• SerialPort issue, How to communicate with a PCI modem ?
• How can I use struct in c#?
• Windows forms help