Problem with simple calculation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am currently using the following

Studio 2005: 8.0.50727.42 (RTM.050727-4200)
..NET Framework: Version 2.0.50727

When doing the following in C#

double a = 11.08;
double b = 11.04;
double c = a - b;

I get 0.0400000000000009 instead of 0.04. Why is this?

Am i missing something?

Thanks
 
Yes, You are missing something. Double is a floating radix datatype, as
defined by the IEEE 754 standard. It is useful for scientific calculations.
If you intend to perform simple decimal calculations, I'd suggest that you
use the Decimal datatype.

The C# newsgroup FAQ is here:
http://www.yoda.arachsys.com/csharp/faq/

This particular item is discussed (in significant detail) here:
http://www.yoda.arachsys.com/csharp/floatingpoint.html

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Back
Top