Home > Backend Development > PHP Tutorial > php sprintf function usage php floating point number format

php sprintf function usage php floating point number format

WBOY
Release: 2016-07-25 08:54:29
Original
1320 people have browsed it
  1. sprintf(" %9.3f", 3.1415926); // Right alignment: If there are not enough digits, use spaces to complete it. Result: "3.142"
  2. sprintf(" %-9.3f", 3.1415926); //Left-aligned: If there are not enough digits, use spaces to complete it. Result: "3.142 "
  3. sprintf(" %.3f", 3.1415926); //Do not specify the total width, result: "3.142"
Copy code

Note:

  1. $num = 100;
  2. sprintf("%.2f", $num );
  3. sprintf("%.2f", (double)$num);
Copy the code

above Are the two results really the same? Although it looks the same, the following reasons may be enlightening. Cause analysis: When the parameter is pushed onto the stack, the caller does not know that the format control character corresponding to num is "%f". When the function is executed, the function itself does not know that what was pushed onto the stack was an integer, so the poor 4 bytes storing the integer $num are forcibly interpreted as a floating point number format, and the whole thing is messed up.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template