According to MSDN, Math.Round will return the following:

Math.Round(4.4); //Returns 4.0.
Math.Round(4.5); //Returns 4.0.
Math.Round(4.6); //Returns 5.0.

So, in my case doing Math.Round(objMyObject.Amount * 100.0) will not do the
trick always. For example:

Math.Round(123.454 * 100.0); // returns 12345
Math.Round(123.455 * 100.0); // returns 12345
Math.Round(123.456 * 100.0); // returns 12346

So I suppose I need to execute this:

Math.Round((objMyObject.Amount * 100.0) - 0.5) to be sure that I get what I
want.

Am I missing something?



-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Peter Ritchie
Sent: Monday, August 25, 2008 12:48 PM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Converting doubles into integers without
rounding errors


As John has alluded to, what you're seeing is a difference in rounding
between formatting a double value as text and multiplying by 100 and
dropping the decimals.

If you have specific rounding logic, perform it before you convert to
int.  If you want the same value that you'd see as formatted text, convert
to text first.

I would recommend something like this:

double tempAmount = objMyObject.Amount * 100.0;
uint nIntAmount = Math.Round(tempAmount);

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to