On May 23, [EMAIL PROTECTED] said:

>Why is the addition of the numbers -67947269.62 and 68288455.49, both with
>only 2 numbers after the decimal, resulting in 341185.86999996 where there
>are 8 numbers after the decimal.  I would expect the number to simply be
>341185.87.  How can i avoid this strange behavior?

perldoc -q decimal

Because although YOU store numbers in decimal, Perl does not, and numbers
like .62 and .49 do not have terminating representations in binary.

0.62 in base 10 is a repeating binary number:

  0.10011110101110000101000111101011100001010001111010111...

So the solution for you is to truncate the number, using something like
sprintf():

  $result = sprintf "%.2f", $n - $m;

perldoc -f sprintf

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to