Code Comments
Programming Forum and web based access to our favorite programming groups.I think I found a bug. If not, I really need someone to explain this
to me.
I have a method that needs to return a Single. I need to round the
value to 2 decimals and this has always worked fine. However, the code
below will always return the Single variable "c" as the original value
of variable "st". I have tried this with decimals, doubles and many
variations only to get the same result. However, if I change the value
of "st" to be one digit less (20294), it works. When I assign the
value to v and cast to a Single, how does it know the original value
before it was rounded? Shouldn't those primitive types have their own
values that are totally disconnected from the original variable "st"?
double st = 120294.719F;
double v = Math.Round(st,2);
Single c = Convert.ToSingle(v);
Console.WriteLine("Value: {0}",c);
Post Follow-up to this messagequote:Just wanted to add that I have the same problem. This is very confusing to m e, as Math.Round() works fine in all my other projects, but currently, for e xample the coee below returns 43.234539031982422 instead of 43.23453.. any i deas? I use c# express 2005, the application makes use of DirectX and float variab les (I dont think that it matters, but this is the only difference to all ot her working projects that I can think of)... double a = Math.Round(43.234534534, 5); //same thing happens with double a = 43.234534534; a = Math.Round(a, 5);
Originally posted by Wayne Fontes I think I found a bug. If not, I really need someone to explain this to me. I have a method that needs to return a Single. I need to round the value to 2 decimals and this has always worked fine. However, the code below will always return the Single variable "c" as the original value of variable "st". I have tried this with decimals, doubles and many variations only to get the same result. However, if I change the value of "st" to be one digit less (20294), it works. When I assign the value to v and cast to a Single, how does it know the original value before it was rounded? Shouldn't those primitive types have their own values that are totally disconnected from the original variable "st"? double st = 120294.719F; double v = Math.Round(st,2); Single c = Convert.ToSingle(v); Console.WriteLine("Value: {0}",c);
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.