zoukankan      html  css  js  c++  java
  • PHP函数number_format()

    PHP的number_format() 函数通过千位分组来格式化数字。

    语法:

    number_format(number,decimals,decimalpoint,separator)

    注释:该函数支持一个、两个或四个参数(不是三个)。

     1 <?php
     2 /* number_format() 函数通过千位分组来格式化数字。
     3 *  该函数支持一个、两个或者四个参数(注意不是三个)
     4 *  number_format(number,decimals,decimalpoint,separator)
     5 *  第一个参数number,必须,要格式化的数字。如果未设置其他参数,则数字会被格式化为不带小数点且以(,)作为千位分隔符
     6 *  第二个参数decimals,可选,规定多少个小数。如果设置了该参数,则使用点号(.)作为小数点来格式化数字;
     7 *  第三个参数,decimalpoint。可选。规定用作小数点的字符串
     8 *  第四个参数,separator.可选.规定用做千位分割符的字符串.仅使用该参数的第一个字符。比如 "xxx" 仅输出 "x"。 注释:如果设置了该参数,那么所有其他参数都是必需的。
     9 */
    10 $number = 10001.999;
    11 $formatNum1 = number_format($number);
    12 echo $formatNum1; //输出10,002
    13 echo "<br/>";
    14 $formatNum2 = number_format($number,2);
    15 echo $formatNum2; //输出10,002.00
    16 echo "<br/>";
    17 $formatNum3 = number_format($number,2,'x','!');
    18 echo $formatNum3; //输出10!002x00
    19 echo "<br/>";
    20 
    21 
    22 /*
    23 * Build the program's capability - define variables and functions...
    24 */
    25 
    26 $item_label = '';    // String
    27 $item_price = 0.0;    // Float
    28 $item_qty = 1;        // Tnteger
    29 $item_total = 0.0;    // Float - set to use function calculate()
    30 
    31 // 定义函数用以计算
    32 function start_calculate(){
    33     global $item_price, $item_qty, $item_total;
    34     $item_price = number_format($item_price,2); //调用number_format()格式化数字
    35     $item_total = number_format(($item_price * $item_qty),2);
    36 }
    37 
    38 // 定义函数用于输出
    39 function itemToString(){
    40     global $item_label, $item_price, $item_qty, $item_total;
    41     return "$item_label: [Price = $$item_price, Quality = $item_qty, Total = $$item_total]";
    42 }
    43 
    44 /*
    45 * Run the program - set data, call methods...
    46 */
    47 $item_label = "Coffee";
    48 $item_price = 3.89;
    49 $item_qty = 2;
    50 start_calculate();
    51 echo itemToString();
    52 
    53 echo "<br/>";
    54 
    55 $item_label = "Chicken";
    56 $itme_price = .80;
    57 $item_qty = 3.5;
    58 start_calculate();
    59 echo itemToString();
    60 
    61 ?>

    声明:代码并没有实际意义,仅供学习和交流使用。

  • 相关阅读:
    P85 实战练习
    Java语句及控制台输入
    P62 实战练习
    运算符、数据类型转换和注释
    Java变量与常量
    Java语法初步实际应用
    JQ 练习题
    JQ 特效下拉列表 写出与css一样的效果
    JQ 全选设定与设置选中
    JQ JSON数据类型
  • 原文地址:https://www.cnblogs.com/mafu/p/13294447.html
Copyright © 2011-2022 走看看