php中sprintf与printf函数用法区别解析 |
下面是一个示例:四舍五入保留小数点后两位 复制代码 代码如下: <?php $num1 = 21; echo sprintf("%0.2f",$num1)."<br />"; //输出 21.00 $num2 = 16.3287; echo sprintf("%0.2f",$num2)."<br />"; //输出 16.33 $num3 = 32.12329; echo sprintf("%0.2f",$num3)."<br />"; //输出 32.12 ?> 解释下 %0.2f 的含义: % 表示起始字符
printf与sprintf的区别 1. printf函数: int printf ( string format [, mixed args [, mixed ...]] ) Produces output according to format , which is described in the documentation for sprintf() . Returns the length of the outputted string. 把文字格式化以后输出,如: 复制代码 代码如下: $name="hunte"; $age=25; printf("my name is %s, age %d", $name, $age); 2. sprintf函数: Returns a string produced according to the formatting string format . 跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样 。 3. print函数: 是函数,可以返回一个值,只能有一个参数 。 int print ( string arg ) Outputs arg . Returns 1 , always. |