Since there seems to be no clear solution so far, I've gone back to the
beginning. I suggest you get more information by writing some code to
display your numbers to higher precision. My expectation is that the
137.89 is actually 137.889999 (probably more nine's). So your code below
converts this to 13788.9999, then truncates to 137.88.
So add a small value (e.g. .0001) first. Then the outcomes for numbers
above and below the intended number look something like:
On Mon, 25 Aug 2008 12:27:52 -0400, Eddie Lascu <elascu@IBIG...>
>I have some objects that contain an amount field that is declared as
>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
>error that is introduced and the integer obtained is off by a cent (plus
>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
>without this nagging rounding error?
>Any help will be appreciated,
>This list is hosted by DevelopMentor® http://www.develop.com >
>View archives and manage your subscription(s) at