控制浮点数打印格式
浮点数的打印和格式控制是sprintf 的一大常用功能,浮点数使用格式符”%f”控制,默认保留小数点后6 位数字,比如:
复制代码 代码如下:
sprintf("%f", 3.1415926); //结果:"3.141593"
但,有时我们希望自己控制打印的宽度和小数位数,这时就应该使用:”%m.nf”格式,其中m 表示打印数字的整体宽度,n 表示小数点后的位数。比如:
复制代码 代码如下:
sprintf(" %9.3f", 3.1415926); //右对齐:位数不够用空格补全。结果:" 3.142"
sprintf(" %-9.3f", 3.1415926); //左对齐:位数不够用空格补全。结果:"3.142 "
sprintf(" %.3f", 3.1415926); //不指定总宽度,结果:"3.142"
注意一个问题
复制代码 代码如下:
$num = 100;
sprintf("%.2f", $num );
sprintf("%.2f", (double)$num);
上面的两个结果真的一样吗?虽然看起来是一样的,但是下面的原因,可能会有所启发.
原因是:参数压栈时调用者并不知道跟num相对应的格式控制符是个”%f”。而函数执行时函数本身则并不知道当年被压入栈里的是个整数,于是可怜的保存整数$num的那4个字节就被不由分说地强行作为浮点数格式来解释了,整个乱套了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!
