Phpsprintf function usage php floating point number format
- Sprintf ("% 9.3f", 3.1415926); // right alignment: fill the number of digits with spaces. Result: "3.142"
- Sprintf ("%-9.3f", 3.1415926); // left alignment: fill the number of digits with spaces. Result: "3.142"
- Sprintf ("%. 3f", 3.1415926); // The total width is not specified. result: "3.142"
Note:
- $ Num = 100;
- Sprintf ("%. 2f", $ num );
- Sprintf ("%. 2f", (double) $ num );
Are the two results the same? Although it looks the same, the following causes may be enlightening. cause analysis: the caller does not know that the format controller corresponding to num is "% f" when the parameter is pressed ". When a function is executed, the function itself does not know that the number pushed to the stack in the current year is an integer, so the four bytes that saved the integer $ num were forcibly interpreted as a floating-point number format. |