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

Re: BUG!! with Single and Math.Round
The suffix F on a literal makes it a float, not a double. Floats have
23 bits of precision, which works out to 8,388,608 in decimal terms.

So, your float constant, 120294.719F is probably being rounded from the
outset. However, remember that doubles and floats aren't stored in
decimal... they're stored in binary. So, the value stored internally is
just an approximation of the decimal value you've entered.

When you then round it to two decimal places, the rounding is, again,
approximate. When you write it out, you're getting more decimal places
than you wanted because the stored value isn't exact. See this article
by John Skeet:

http://www.yoda.arachsys.com/csharp/floatingpoint.html

If you really want to print out exactly two decimal places, you have
two choices:

1. Use the format specifier in the WriteLine to indicate two decimal
places.

2. Copy the value into a decimal type, round that to two decimals, then
print it out. decimal values are stored in base 10: in decimal, so
rounding to two decimal places really does round to two decimal places.
See Jon's other article:

http://www.yoda.arachsys.com/csharp/decimal.html


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


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 06:32 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.