I don't see a Double.Truncate but a Math.Truncate and that's not of much
help because it returns a new double and not an integer. the rounding
problem can reoccur when storing that new number...



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


Personally, I've never used it before, but I see in framework 2.0 there is
the option of:

(int) Double.Truncate(objMyObject.Amount * 100);

HTH

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Eddie Lascu
Sent: 25 August 2008 06:28 PM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Converting doubles into integers without rounding
errors

Hello everyone,

I have some objects that contain an amount field that is declared as double.
Since it contains amounts, it always has only two decimal digits that are
significant. During the process I need to convert that double into an
integer by removing the decimal point. For example, $78.59 should be
converted to integer 7859 and $101.53 to 10153. in my code I have
uint nIntAmount = (uint)(objMyObject.Amount * 100);

The problem I am facing is that sometimes, very rarely, there is a rounding
error that is introduced and the integer obtained is off by a cent (plus or
minus). For example, this is a line that was traced in my log file:

"Updating the batch with $137.89 as the amount in the transaction. This
amount was converted to 13788."

Can either of you suggest a different way to convert the amounts in integers
without this nagging rounding error?

Any help will be appreciated,

Eddie

===================================
This list is hosted by DevelopMentorR  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

===================================
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