As stated by dragonchild, representation of floating point numbers is inexact. I added three lines to your program that show this.

#! /usr/bin/perl -w use strict; for( 0..9 ) { my $num0 = "1.$_"; my $num1 = "1.0$_"; my $num2 = "1.00$_"; #next three lines were added $num0 -= 1; $num1 -= 1; $num2 -= 1; printf "$num0 => %0.0f $num1 => %0.1f $num2 => %0.2f\n", $num0, $num1, $num2; }

This produces the following output.

0 => 0 0 => 0.0 0 => 0.00 0.1 => 0 0.01 => 0.0 0.00099999999999989 => 0.00 0.2 => 0 0.02 => 0.0 0.002 => 0.00 0.3 => 0 0.03 => 0.0 0.00299999999999989 => 0.00 0.4 => 0 0.04 => 0.0 0.004 => 0.00 0.5 => 0 0.05 => 0.1 0.00499999999999989 => 0.00 0.6 => 1 0.0600000000000001 => 0.1 0.00600000000000001 => 0.01 0.7 => 1 0.0700000000000001 => 0.1 0.0069999999999999 => 0.01 0.8 => 1 0.0800000000000001 => 0.1 0.00800000000000001 => 0.01 0.9 => 1 0.0900000000000001 => 0.1 0.0089999999999999 => 0.01
--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

In reply to Re: counter-intuitive sprintf behaviour by pfaut
in thread counter-intuitive sprintf behaviour by grinder

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.