Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

BUG!! with Single and Math.Round
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);


Report this thread to moderator Post Follow-up to this message
Old Post
Wayne Fontes
05-26-05 08:57 AM


Re: BUG!! with Single and Math.Round
quote:
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);
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);

Report this thread to moderator Post Follow-up to this message
Old Post
thkoe002
08-06-06 10:31 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C# archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 10:28 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.