Would Math.Floor() help more in your case? Would round down to the
nearest int.
-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:
ADVANCED-DOTNET@DISC...] On Behalf Of Eddie Lascu
Sent: Monday, August 25, 2008 13:04
To:
ADVANCED-DOTNET@DISC...
Subject: Re: Converting doubles into integers without rounding errors
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:
ADVANCED-DOTNET@DISC...]On Behalf Of Peter Ritchie
Sent: Monday, August 25, 2008 12:48 PM
To:
ADVANCED-DOTNET@DISC...
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(r)
http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor(r)
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
opensubscriber is not affiliated with the authors of this message nor responsible for its content.