in reply to printf rounding issue

You are printing with 2 digits after the . but the first non zero digit is the third, so of course it can't be displayed. And ++LanX on the two links :)

Replies are listed 'Best First'.
Re^2: printf rounding issue
by pryrt (Abbot) on Nov 16, 2016 at 14:25 UTC

    vishy_acts,

    To expand on ++Eily's post: you probably need to read more about the print FORMAT specifiers at sprintf. Specifically, the value 2 after the the . in your %2.2f indicates a precision of two (hence Eily's statement, "2 digits after the .")

    Regarding your statement, "Also what is the solution to print value correctly in FILE". You haven't told us what you want the data to look like in FILE, so we're not likely to guess what you mean by "correctly". Do you want all your data with only two digits after the decimal point, which is what your printf currently claims? Then it's already printing correctly. Do you want it to always print 3 digits after the decimal point? %.3f. Do you want printf to always use its default number of digits after the decimal point? %f will use its default (which is typically 6 digits). Do you want the print-style "we've got a secret algorithm to determine how many digits to print"? Then use print FILEHANDLE "$val ", which will use the exact same rules as the print "$val " you're already sending to STDOUT (ie, don't use printf to format the number, since you want perl to choose the format for you). Do you want to use scientific notation with two digits after the decimal place (1.00e-3)? Use %.2e.

    To get it to print correctly, you have to tell perl what 'correctly' means. If you do not know how to tell perl what 'correctly' means, you have to at least tell us what 'correctly' means, so we can help you learn how to tell perl how you want it printed.