下面是一个示例:四舍五入保留小数点后两位

 代码如下 复制代码

<?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 www.111Cn.net
?>

解释下 %0.2f 的含义:

% 表示起始字符
0 表示空位用0填满
2 表示小数点后必须占两位
f 表示转换成浮点数

转换字符
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% 印出百分比符号,不转换。
b 整数转成二进位。
c 整数转成对应的 ASCII 字元。
d 整数转成十进位。
f 倍精确度数字转成浮点数。
o 整数转成八进位。
s 整数转成字串。
x 整数转成小写十六进位。
X 整数转成大写十六进位。

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函数:
string sprintf ( string format [, mixed args [, mixed ...]] )

Returns a string produced according to the formatting string format .

跟printf相似,但不打印,而是返回格式化后的文字,其他的与printf一样。

3. print函数:

是函数,可以返回一个值,只能有一个参数。

int print ( string arg )

Outputs arg . Returns 1 , always.

 

php中sprintf与printf函数用法区别的更多相关文章

  1. jquery中attr()与prop()函数用法实例详解(附用法区别)

    本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...

  2. php中strstr、strrchr、substr、stristr四个函数用法区别

    php中strstr.strrchr.substr.stristr四个函数用法区别: php中strstr strrchr substr stristr这四个字符串操作函数特别让人容易混淆,常用的是s ...

  3. html中的alt和title用法区别

    html中的alt和title用法区别 首先明确一下概念,alt是html标签的属性,而title既是html标签,又是html属性.title标签这个不用多说,网页的标题就是写在<title& ...

  4. PHP中fopen,file_get_contents,curl函数的区别

    PHP中fopen,file_get_contents,curl函数的区别 1.fopen/file_get_contents每次请求都做DNS查询,并不对DNS的信息进行缓存,而curl会对DNS的 ...

  5. Oracle中 (+)与left join 的用法区别

    Oracle中 (+)与left join 的用法区别 原创 2017年01月11日 13:33:42 6648 select * from a,b where a.id=b.id(+); (+)写在 ...

  6. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  7. STM32中如何对printf函数重定向

    通过USART1向计算机的串口调试助手打印数据,或者接收计算机串口调试助手的数据,接下来我们现STM32工程上的printf()函数,方便用于程序开发中调试信息的打印. 方法一:使用MicroLIB库 ...

  8. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  9. jquery中this与$(this)的用法区别

    jquery中this与$(this)的用法区别.先看以下代码: $("#textbox").hover( function() { this.title = "Test ...

随机推荐

  1. replace into

    讨人喜欢的 MySQL replace into 用法(insert into 的增强版) 在向表中插入数据的时候,经常遇到这样的情况:1. 首先判断数据是否存在: 2. 如果不存在,则插入:3.如果 ...

  2. nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog

    spring单元測试时发现的问题: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsin ...

  3. 通过输入方式在Android上进行微博OAuth登录

    在微博认证方式里,基本的OAuth认证是必须要调整到跳转到第三方页面上进行授权的,例如下面的例子:     1.从http://open.weibo.com/wiki/index.php/SDK#An ...

  4. [Angular2 Form] Understand the Angular 2 States of Inputs: Pristine and Untouched

    Angular 2’s ngModel exposes more than just validity, it even gives you the states of whether the inp ...

  5. Java final修饰形参

    转自:http://java.chinaitlab.com/base/836044.html public class BB{ public int i; } public class PP{ pub ...

  6. CSDN蒋涛:我为什么和王峰一起创办极客帮天使基金?

         i 黑马 记者:王静静 7月15日,i黑马在一家咖啡厅见到了CSDN创始人蒋涛,这位中国最大的程序猿社区的创始人,正在经营一份新事业,他和蓝港在线创始人王峰正式成立了天使基金"极客 ...

  7. Media Queries详细

    @media only screen and (max-device-width: 480px) { //页面最大宽度480px } <link rel="stylesheet&quo ...

  8. oc-25-id类型

    /** id:万能指针 能够指向任何OC对象. id = NSObject *, id cat = [Cat new]; [cat jump]; NSObject:是所有类的父类,基类.可以指向任何O ...

  9. 字典树(Trie)的java实现

    一.定义 字典树又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用 ...

  10. Eclipse目录

    1. 解决Ubuntu下的Eclipse打开Windows编写的java代码的中文乱码 2. Eclipse常用快捷键