As Perl is based on c, let's look at how c handles this. In c, you can do:
float x = 147.2; printf("%20.20f", x * 100);
it gives you 14719.99969482421875000000, which is fine, unless you are working on some finance application (COBOL will not have this problem, as its decimal is fixed; In Java you will have same thing as c, as Java is also c based, just like Perl, but Java has a class called BigDecimal, which holds fixed decimal.)
However, when you try
float x = 147.2; printf("%d", x * 100);
It produces -167772160, which is garbage, but it is not a c problem, it is the programmer's problem, as c is trying to interprete that piece of memory as integer, as the programmer required.
To resolve this problem, when they develop Perl, they did this:
float x = 147.2; printf("%d", (int)(x * 100));
which prints out 14719.
So a new problem is introduced. However this would be fine to most people, most of the time. If you really care the accuracy and precision, because of the nature of your application, then try Math::BigFloat.

In reply to Re: floating points and sprintf by Anonymous Monk
in thread floating points and sprintf by kennethwlangley

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.