tags:

views:

37

answers:

4

I now want to know that means this line:

printf("Answer: %00010.6f", 22);

He prints: 022.000000. But way? i know whar 6f means float.

thansk for answers

A: 

printf("Answer: %f", 22) would enter the number 22 to the string "Answer: %f" in the place of the "%f" and print it as a float ("f" is for "float"). The numbers between the "%" and the "f" are to set the format - the number of digits that the printout will have.

Rivka
+1  A: 

The printf() syntax and meanings are very well-documented. Look at the printf(3) man page or the Wikipedia printf entry.

The particular example you gave means: print a floating point number. Give it 6 characters after the decimal point. Then prefix it with zeros until it is at least 10 characters.

Karmastan
A: 

this format string means:

  • use 0 as filling character
  • fill to a minimum of 3 characters before the . ( 2 -> 002, 23 -> 023, longer numbers stay as they are)
  • display exactly 10 characters (including the delimiter)
  • use . as decimal delimiter
  • display 6 digits after the delimiter
  • type float
Sven Koschnicke
The width specifier (10 in this case) is not a minimum but the actual width. printf("%00010.6f\n", 22.1234567891234); displays 022.123457
GreenMatt
thank you, i changed that
Sven Koschnicke
+2  A: 

After initially thinking this was C (result of long habit), I realize this is for PHP. Mostly the same, but the constant seems to be handled differently.

Anyway, the parameters in your code break down as follows:

  • f = print the number as a floating point
  • 10 = total field width of ten digits
  • 000 = print up to 3 leading zeros when applicable (i.e. if there aren't 3 significant figures left of the decimal point)
  • . = use a dot as the decimal separator
  • 6 = six places after the decimal

It seems the printing parameters for PHP's printf are actually on the sprintf page.

GreenMatt