Problem with simple calculation

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
 
N

Nick Malik [Microsoft]

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.
 

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