weird problems with float and double : Why 262.4 - 313.83 = -51.4299927 ???

M

Matthieu Tudury

Hi, i got a weird problem with decimal numbers,

Here is a test sample :

float a, b, c;
double da, db, dc;

da = 313.83;
db = 262.4;
dc = db -da;

a = (float)da;
b = (float)db;
c = b - a;


Console.WriteLine(c.ToString());
Console.WriteLine(dc.ToString());

DataSet ds = new DataSet();
ds.Tables.Add("titi");
ds.Tables[0].Columns.Add("number",typeof(float));
ds.Tables[0].Columns.Add("number2",typeof(double));
DataRow dr = ds.Tables[0].NewRow();

dr[0] = c;
dr[1] = dc;
ds.Tables[0].Rows.Add(dr);

Console.WriteLine(ds.GetXml());

and the result :

-51,42999
-51,43
<NewDataSet>
<titi>
<number>-51.4299927</number>
<number2>-51.430000000000007</number2>
</titi>
</NewDataSet>


If Someone can help me solve this problem...

Thanks
 

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