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.


In reply to Re^4: perl subtraction by dsheroh
in thread perl subtraction by jagdish.eashwar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.