Thursday, 15 April 2010

integer - PHP printf function and it's weird behaviour -



integer - PHP printf function and it's weird behaviour -

i'm baffled these numbers mean. me seems printf gives me wrong results.

echo printf("%.2f", 1); // 1.004 echo printf("%.3f", 1); // 1.005 echo printf("%.2f", 1.1234); // 1.124

first of seems print many decimals , have no thought numbers are. can shed lite on matter?

simple. printf() has homecoming value, integer. , value - length of resulting string. thus, code doing 2 things:

first, format & output string printf() second, echo() result, length each string.

that because see 1.004 first case, example. it's 1.00 4 (and length of "1.00" string 4)

if intention print formatted string, either utilize printf() is:

printf("%.2f", 1);

or utilize sprintf() echo:

echo sprintf("%.2f", 1);

php integer printf

No comments:

Post a Comment