Click here to Skip to main content
16,005,038 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: How to round a double correctly... Pin
CCG311-Nov-08 7:39
CCG311-Nov-08 7:39 
AnswerRe: How to round a double correctly... Pin
sph3rex11-Nov-08 5:16
sph3rex11-Nov-08 5:16 
GeneralRe: How to round a double correctly... Pin
CCG311-Nov-08 5:21
CCG311-Nov-08 5:21 
GeneralRe: How to round a double correctly... Pin
Fabio V Silva11-Nov-08 5:25
Fabio V Silva11-Nov-08 5:25 
GeneralRe: How to round a double correctly... Pin
CCG311-Nov-08 5:31
CCG311-Nov-08 5:31 
GeneralRe: How to round a double correctly... Pin
Dave Kreskowiak11-Nov-08 7:07
mveDave Kreskowiak11-Nov-08 7:07 
GeneralRe: How to round a double correctly... Pin
CCG311-Nov-08 7:25
CCG311-Nov-08 7:25 
GeneralRe: How to round a double correctly... Pin
Dave Kreskowiak11-Nov-08 8:02
mveDave Kreskowiak11-Nov-08 8:02 
CCG3 wrote:
Doesn't MidpointRounding.AwayFromZero fix what you are refering to?


No, it doesn't. It just happened to change the behavior of the Round method so THAT particular case worked out properly. The AwayFromZero option rounds numbers up, never down.


CCG3 wrote:
And now my return is .03 just as I would expect it to be. I am only asking because accuracy is important and it seems to be pretty accurate as of now. But are you saying if I had 1.255 / 100 then it wouldn't be accurate?


Yep. The only reason why it worked is because the math using Double numbers worked out to nice even binary representations. For example, 1.255 / 100 = 0.012549999999999, not 0.01255. In your case, 2.5 / 100 worked out evenly to 0.025. It's those binary approximations that screws up the rounding. It just happened to work out properly on the example you tried. It will NOT work in all cases.

You'd probably be better off using the Decimal type instead of Double.


A guide to posting questions on CodeProject[^]



Dave Kreskowiak
Microsoft MVP
Visual Developer - Visual Basic
     2006, 2007, 2008




GeneralRe: How to round a double correctly... Pin
CCG311-Nov-08 8:20
CCG311-Nov-08 8:20 
GeneralRe: How to round a double correctly... Pin
Dave Kreskowiak11-Nov-08 9:48
mveDave Kreskowiak11-Nov-08 9:48 
General[Message Deleted] Pin
CCG311-Nov-08 7:32
CCG311-Nov-08 7:32 
GeneralRe: How to round a double correctly... Pin
Jon_Boy11-Nov-08 8:15
Jon_Boy11-Nov-08 8:15 
AnswerRe: How to round a double correctly... Pin
ian dennis17-Nov-08 11:14
ian dennis17-Nov-08 11:14 
QuestionBorderless Form Pin
Gagan.2010-Nov-08 23:13
Gagan.2010-Nov-08 23:13 
AnswerRe: Borderless Form Pin
Thomas Stockwell11-Nov-08 2:47
professionalThomas Stockwell11-Nov-08 2:47 
GeneralRe: Borderless Form Pin
Gagan.2011-Nov-08 4:08
Gagan.2011-Nov-08 4:08 
GeneralRe: Borderless Form Pin
Thomas Stockwell11-Nov-08 4:31
professionalThomas Stockwell11-Nov-08 4:31 
QuestionNeed Help In Vb.net Pin
sarfarazaliqureshi10-Nov-08 22:18
sarfarazaliqureshi10-Nov-08 22:18 
AnswerRe: Need Help In Vb.net Pin
Gagan.2010-Nov-08 23:04
Gagan.2010-Nov-08 23:04 
GeneralRe: Need Help In Vb.net Pin
sarfarazaliqureshi11-Nov-08 8:04
sarfarazaliqureshi11-Nov-08 8:04 
AnswerRe: Need Help In Vb.net Pin
Thomas Stockwell11-Nov-08 2:49
professionalThomas Stockwell11-Nov-08 2:49 
GeneralRe: Need Help In Vb.net Pin
sarfarazaliqureshi11-Nov-08 8:09
sarfarazaliqureshi11-Nov-08 8:09 
GeneralRe: Need Help In Vb.net Pin
Thomas Stockwell11-Nov-08 8:17
professionalThomas Stockwell11-Nov-08 8:17 
GeneralRe: Need Help In Vb.net Pin
sarfarazaliqureshi11-Nov-08 9:06
sarfarazaliqureshi11-Nov-08 9:06 
Questionreading weighing scale data Pin
Ahamed Azeem10-Nov-08 20:36
Ahamed Azeem10-Nov-08 20:36 

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.