<?php
$num = 8.16789; //第一种:利用round()对浮点数进行四舍五入
echo round($num,2).PHP_EOL; //8.17 //第二种:利用sprintf格式化字符串
$format_num = sprintf("%.2f",$num);
echo $format_num.PHP_EOL; //8.17 //第三种:利用千位分组来格式化数字的函数number_format()
echo number_format($num, 2).PHP_EOL; //8.17
//或者如下
echo number_format($num, 2, '.', '').PHP_EOL; //8.17
15 //或者如下
16 echo number_format(2,2);//2.00 以千位分隔符方式格式化一个数字 不夠自動補0

下面看下PHP中对一些商品的价格计算或价格的的展示,需要精确到小数点后的两位数字,也就是我们平时RMB中的分的单位。

那在PHP中如何展示商品的价格,并保留到分的单位的呢?

PHP number_format() 函数

number_format():函数可以通过千位分组的形式来格式化数字。

语法:

 number_format(number,decimals,decimalpoint,separator)

参数:

number:必需。要格式化的数字。

decimals:可选。规定多少个小数。

decimalpoint:可选。规定用作小数点的字符串。

separator:可选。规定用作千位分隔符的字符串。

例:PHP商品价格以元为单位,保留两位小数

 <?php
$a = 10;
echo number_format($a, '2') . "<br/>"; $b = 100000;
echo number_format($b, '2') . "<br/>"; $c = 5458.5684;
echo number_format($c, '2') . "<br/>"; $d = '1254.8963';
echo number_format($d, '2') . "<br/>"; $e = '88.9643';
echo number_format($e, '2') . "<br/>"; 分别输出:
10.00
100,000.00
5,458.57
1,254.90
88.96

以上示例总结:

1、无论是数字类型或是字符串类型的数字,都可以被 number_format() 函数操作

2、number_format() 在操作不含有小数的数字时,如果设置了有多小个小数,会以 0 的形式补充。

3、如果操作带有多少小数的数字,会以四舍五入的方式进行最近一位的取值

4、如果不设置 number_format() 第三个和第四个参数,整数部分如果大于3位,那么从小数点左边向右开始,每三位都用','号分割

php保留两位小数的3种方法的更多相关文章

  1. (后台)Java:对double值进行四舍五入,保留两位小数的几种方法

    mport java.text.DecimalFormat; DecimalFormat df = new DecimalFormat("######0.00"); double ...

  2. Java:对double值进行四舍五入,保留两位小数的几种方法

    1. 功能 将程序中的double值精确到小数点后两位.可以四舍五入,也可以直接截断. 比如:输入12345.6789,输出可以是12345.68也可以是12345.67.至于是否需要四舍五入,可以通 ...

  3. Python中保留两位小数的几种方法

    https://blog.csdn.net/Jerry_1126/article/details/85009810 保留两位小数,并做四舍五入处理方法一: 使用字符串格式化>>> a ...

  4. Double值保留两位小数的四种方法

    public class DoubleTest { //保留两位小数第三位如果大于4会进一位(四舍五入) double f = 6.23556; /** *使用精确小数BigDecimal */ pu ...

  5. PHP保留两位小数的几种方法

    $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2); //10.46 //第二种:利用sprintf格式化字符串 $format_ ...

  6. JS保留两位小数的几种方法

    四舍五入 以下处理结果会四舍五入: var num =2.446242342; num = num.toFixed(2); // 输出结果为 2.45 不四舍五入 以下处理结果不会四舍五入: 第一种, ...

  7. php 基础 PHP保留两位小数的几种方法

    $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2); //10.46 //第二种:利用sprintf格式化字符串 $format_ ...

  8. JAVA 保留两位小数的四种方法

    import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; publiccl ...

  9. php取两位小数的几种方法

    php取两位小数的几种方法 一.总结 一句话总结: 1.round   四舍五入 2.sprintf   c语言方式 3.number_format 千分位数字格式化的那个函数 二.php取两位小数的 ...

随机推荐

  1. poj1742(多重背包分解+01背包二进制优化)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  2. L295 how to turn down a job but keep a good relationship with the hiring manager

    Let’s say you’re on the hunt for a new job. Three interviews in, you realize it’s not the place for ...

  3. ChinaCock界面控件介绍-TCCImageViewerForm

    有多个图片,左右滑动可以切换,通过手势还可以放大.缩小查看,象常见的相册,就是这样子实现效果. 现在,我们有了TCCImageViewerForm组件,也可以轻松实现这样的场景应用. 现在看看TCCI ...

  4. 如何给cbv的程序添加装饰器

    引入method_decorator模块 1,直接在类上加装饰器 @method_decorator(test,name=‘dispatch’) class Loginview(view) 2,直接在 ...

  5. Day16作业及默写

    hashlib模块,写函数校验两个文件是否内容相同(如果这两个文件很大) import hashlib md5 = hashlib.md5() md5.update(b'hello') md5.upd ...

  6. TCP/IP协议的四个层及作用

  7. hmtl工具

    在线编辑器:http://runjs.cn/code 关注微信小程序

  8. Python 命名元组

    from collections import namedtuple # 类 p = namedtuple("Point", ["x", "y&quo ...

  9. 【Python】Excel-4(样式设置)

    #练习: #封装一个ExcelUtil的模块(构造函数是excel的路径),里面提供封装的方法: #1 获取某个sheet对象 #2 打印所有sheet名称 #3 给某个sheet的某个单元格写入内容 ...

  10. js-null 、undefined

    注意:null 转为数值时为0,而 undefined 转为数值时为 NaN. Number(undefined) // NaN Number(null) // 0 null 表示"没有对象 ...