I think the mistake is to use .5. As Greg pointed out, the error is very
small, and your problem is when that produces a number less than your
expected integer.

So multiply by 100, add a small value (typically .0001), and truncate by
converting to an int. That will give you an int in cents.

(If you had to deal with negative numbers, you would have to reverse the
sign first, since your add of a small amount must go away from zero.)

Of course if you had more control, you should use int's or decimal's
rather than doubles for amounts.

David.



On Mon, 25 Aug 2008 10:52:23 -0700, Greg Young <[EMAIL PROTECTED]>
wrote:

>.005 is ALOT for a double to be off ... usually its more like .0000000001
off
>
>On Mon, Aug 25, 2008 at 10:49 AM, Eddie Lascu <[EMAIL PROTECTED]> wrote:
>> The problem is that I have to multiply by 100 before rounding, so the
double
>> must no be off by more than 0.005. For example, if 4.56 is actually
stored
>> as 4.5610, the number multiplied by 100 will be 456.100 which gets to
>> 456.600 when adding 0.5 which rounds up to 457.
>>
>> I feel like my brain is about to explode...
>>
>>
>> -----Original Message-----
>> From: Discussion of advanced .NET topics.
>> [mailto:[EMAIL PROTECTED] Behalf Of Terry Griffin
>> Sent: Monday, August 25, 2008 1:27 PM
>> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
>> Subject: Re: [ADVANCED-DOTNET] Converting doubles into integers without
>> rounding errors
>>
>>
>> The problem with going from a double to an int is that the final digit
can
>> be cut off because the double is inexact. But the double will be close.
>> All you need to do is add 0.5 to the value before doing the conversion
to
>> an int. The "extra" 0.5 will make a value like 4.999999999 get converted
>> to 5 instead of 4. Unless your original numbers are off by a large
amount
>> (close to 0.5) this won't introdcue any error.
>>
>> int intVal;
>> double dblVal;
>>
>> intVal = (int)(dbleVal * 100 + 0.5);
>>
>>
>>
>> Terry Griffin
>> Sr. Software Engineer
>> Carl Zeiss SMT Inc.
>> ALIS Business Unit
>> 1 Corporation Way
>> Peabody, MA 01960 USA
>> [EMAIL PROTECTED]
>> (978) 826-1569
>>
>>
>>
>>
>> Daniel Barla-Szabo <[EMAIL PROTECTED]>
>> Sent by: "Discussion of advanced .NET topics."
>> <ADVANCED-DOTNET@DISCUSS.DEVELOP.COM>
>> 08/25/2008 01:10 PM
>> Please respond to
>> "Discussion of advanced .NET topics."
>> <ADVANCED-DOTNET@DISCUSS.DEVELOP.COM>
>>
>>
>> To
>> ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
>> cc
>>
>> 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 DevelopMentorĀ®  http://www.develop.com

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

Reply via email to