zoukankan      html  css  js  c++  java
  • 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');
    $b = 1000000;
    echo number_format($b,'2');
    $c = 5458.5684;
    echo number_format($c,'2');
    $d = '1254.8963';
    echo number_format($d,'2');
    $e = '88.9643';
    echo number_format($e,'2');
    ?>
    10.00
    1,000,000.00
    5,458.57
    1,254.90
    88.96

    以上示例总结:

    1、无论是数字类型或是字符串类型的数字,都可以被 number_format() 函数操作
    2、number_format() 在操作不含有小数的数字时,如果设置了有多小个小数,会以 0 的形式补充。
    3、如果操作带有多少小数的数字,会以四舍五入的方式进行最近一位的取值
    4、如果不设置 number_format() 第三个和第四个参数,整数部分如果大于3位,那么从小数点左边向右开始,每三位都用','号分割

    例 PHP number_format() 去掉整数部分的分割符号

    代码:

    <?php
    echo number_format("1000000",2,".","");
    echo number_format("1000000",2,".","x");
    echo number_format("1000000",2,"y","x");
    ?>
    1000000.00
    1x000x000.00
    1x000x000y00

    以上示例总结:

    1、number_format() 函数的第三个参数,可以替换小数字的展示方式,比如把小数点换成y
    2、number_format() 函数的第四个参数,可以替换整数部分的千分位分割符号,比如为空,或为X
    3、要注意的是,number_format() 函数的第三个参数与第四个参数是共同存在的,不能只填写一个。

    踩过这个坑,还有下一个坑等着你,这一路就是给自己填坑,坑填多了,也就习惯了,直到这一路平坦了,也就无怨无悔了。
  • 相关阅读:
    jqgrid 获取选中用户的数据插入
    jqgrid 自定义文本框、选择框等查询
    Java学习—— for循环
    Android中 Http请求
    异步消息处理机制——Handler用法
    ThreadLocal
    Android开发 学习笔记——HelloWorld
    eclipse 常用插件
    mysql 命令备份还原
    学习
  • 原文地址:https://www.cnblogs.com/xiaofeilin/p/14047831.html
Copyright © 2011-2022 走看看