Click here to Skip to main content
15,922,325 members
Home / Discussions / C#
   

C#

 
AnswerRe: Math.Round..What am I missing? Pin
benjymous18-Dec-07 4:58
benjymous18-Dec-07 4:58 
GeneralRe: Math.Round..What am I missing? Pin
jayart18-Dec-07 5:03
jayart18-Dec-07 5:03 
GeneralRe: Math.Round..What am I missing? Pin
Stevo Z18-Dec-07 5:27
Stevo Z18-Dec-07 5:27 
GeneralRe: Math.Round..What am I missing? Pin
jayart18-Dec-07 5:35
jayart18-Dec-07 5:35 
AnswerRe: Math.Round..What am I missing? Pin
led mike18-Dec-07 5:07
led mike18-Dec-07 5:07 
GeneralRe: Math.Round..What am I missing? Pin
jayart18-Dec-07 5:32
jayart18-Dec-07 5:32 
AnswerRe: Math.Round..What am I missing? Pin
dipak.dipak18-Dec-07 5:24
dipak.dipak18-Dec-07 5:24 
GeneralRe: Math.Round..What am I missing? Pin
Luc Pattyn18-Dec-07 5:55
sitebuilderLuc Pattyn18-Dec-07 5:55 
Sorry, you're mistaken. Your example happens to produce the result you want, but it is
not correct. Here is the background:

The way floats/doubles are stored in binary makes it impossible to exactly represent
most values, especially the ones that seem like round numbers to humans thinking
using base ten.

So 4, 2, 1, 0.5, 3.5, 3.75 and many others are representable exactly (basically since
they all equal an integer divided by some power of 2); but 3.6 and 3.8 and an infinite
number of other values are not.

Now whatever float/double function you call, if it returns a float/double will suffer
from the same phenomenon. So the only way to really get "3.6" or "3.8" is by using a
function that not only rounds but returns a string. I trust there are some formatting
methods that do just that in every programming language; In .NET languages
ToString() should be one of them, given an appropriate format specifier.

If you want to know much more about this topic, here it is:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

BTW: to circumvent the floating-point rounding problem, especially for monetary
numbers, they introduced the "decimal" data type (which basically stores decimal
digits).

Smile | :)
Luc Pattyn [Forum Guidelines] [My Articles]


Sorry for any delays in replying, I currently don't always get e-mail notifications.


Questionquestion about FrameWork 3.5 Pin
E_Gold18-Dec-07 4:07
E_Gold18-Dec-07 4:07 
GeneralRe: question about FrameWork 3.5 Pin
Colin Angus Mackay18-Dec-07 4:31
Colin Angus Mackay18-Dec-07 4:31 
GeneralRe: question about FrameWork 3.5 Pin
Paul Conrad24-Dec-07 19:52
professionalPaul Conrad24-Dec-07 19:52 
QuestionHttpWebRequest ? Pin
cd_dvd18-Dec-07 3:59
cd_dvd18-Dec-07 3:59 
GeneralRe: HttpWebRequest ? Pin
Vasudevan Deepak Kumar18-Dec-07 4:12
Vasudevan Deepak Kumar18-Dec-07 4:12 
GeneralRe: HttpWebRequest ? Pin
cd_dvd18-Dec-07 4:31
cd_dvd18-Dec-07 4:31 
GeneralRuntime type casting Pin
Skippums18-Dec-07 3:23
Skippums18-Dec-07 3:23 
GeneralRe: Runtime type casting Pin
CKnig18-Dec-07 3:29
CKnig18-Dec-07 3:29 
GeneralRe: Runtime type casting Pin
CKnig18-Dec-07 3:32
CKnig18-Dec-07 3:32 
GeneralRe: Runtime type casting Pin
Skippums18-Dec-07 6:37
Skippums18-Dec-07 6:37 
GeneralRe: Runtime type casting Pin
m@u18-Dec-07 3:35
m@u18-Dec-07 3:35 
GeneralRe: Runtime type casting Pin
Skippums18-Dec-07 5:56
Skippums18-Dec-07 5:56 
GeneralDetermine User Role (Vista) Pin
Stevo Z18-Dec-07 3:10
Stevo Z18-Dec-07 3:10 
GeneralRe: Determine User Role (Vista) Pin
Peter Walburn26-Feb-10 4:38
Peter Walburn26-Feb-10 4:38 
GeneralTAPI 2 Pin
baerten18-Dec-07 3:09
baerten18-Dec-07 3:09 
GeneralConvert to int Pin
eyeseetee18-Dec-07 1:55
eyeseetee18-Dec-07 1:55 
GeneralRe: Convert to int Pin
J4amieC18-Dec-07 1:59
J4amieC18-Dec-07 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.