in reply to printf rounding issue

That's a classic, see for instance perl floating number addition and following discussion.

Especially Humans have too many fingers! :)

In short, the internal representation of numbers is in binary system, which can't do all decimal fractions without rounding error.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: printf rounding issue
by vishy_acts (Novice) on Nov 16, 2016 at 13:54 UTC
    I am doing same thing as mentioned in the thread .
    $val = $val * 100 ; $val = $val / 100;
    This is the reason I am getting correct value when I am printing on screen with print . My issue is why I am getting different values when I am using print and printf . Also what is the solution to print value correctly in FILE .
      > I am doing same thing as mentioned in the thread .
      $val = $val * 100 ; $val = $val / 100;

      from the thread:

      The shift must be a string operation. Dividing by 100 reintroduces the problem with floats!

      edit

      > My issue is why I am getting different values when I am using print and printf

      as Eily already told you, you are rounding in printf at the second position not the third.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!