in reply to perl subtraction

You could also use printf to format the value reported. Try something like this:

printf ("\$%2.2f\n", $val);

where $val is the value that you want to print. This will give you dollar sign, and value to two decimal places. Your first example comes back as $0.00, which is probably what you want. This might be a little easier to deal with than changing over to representing your figures in cents, if you've already written some code.

Replies are listed 'Best First'.
Re^2: perl subtraction
by Anonymous Monk on Jan 10, 2010 at 07:14 UTC
    Hi,

    Thanks. What I understand now is that the internal base 2 representation is not likely to cause any problems if I am dealing with only integers. In which case, I can convert the amounts to cents in the section of the code where I check whether the two amounts are equal. Thanks again.

      What I understand now is that the internal base 2 representation is not likely to cause any problems if I am dealing with only integers.

      Yes, as long as they're not too large. If you keep the numbers small than ±9,007,199,254,740,992, you'll be ok.

      >perl -e"printf qq{%.0f\n}, 9007199254740992 + 3" 9007199254740996 even? how odd!

      Have fun calculating tax without using decimals, though. What you want to do then is perform rounding:

      # If working with cents $tax_amount = sprintf('%.0f', $amount * $tax_rate);
      Hi,

      I'm afraid that didn't work out as I expected. I multiplied the amounts by 100 to get the amounts in cents. But when I compare the two, my script is still telling me that they are not equal./p>

      use warnings; use strict; my($amt1,$amt2,$mytot,$perltot,$diff); print "Enter amt1 : "; chomp($amt1 = <STDIN>); print "\nEnter amt2 : "; chomp($amt2 = <STDIN>); print "\nEnter the total : "; chomp($mytot = <STDIN>); $mytot = $mytot * 100; # convert to cents $perltot = ($amt1 * 100) + ($amt2 * 100); #convert to cents $diff = $perltot - $mytot; print "$mytot - $perltot = $diff\n
        Just multiplying by 100 doesn't make the errors go away. You still need to round off the result to produce an integer.

        Consider:

        If you have the value 1/3, or 0.333333333... (using a periodic decimal number rather than a periodic binary number for simplicity), of a dollar and you multiply it by 100, you get 33.333333... cents, which is still not equal to 33 cents. You have to round the 33.333333... off to just 33 before they'll be equal.

        In this particular example, you could use int $val to convert the floating-point 33.333333... to the integer 33, but, in the general case, you'd probably want to use int ($val + 0.5) for simple rounding like I was taught in elementary school (always round 1/2 up), assuming you're only working with positive values. If you have to cope with negative numbers or if the accumulation of errors from always rounding 0.5 up would be an issue, then you should check CPAN for modules which implement a rounding technique appropriate to your needs.