茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1664574 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
HoneyMoon_Day2_00053.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

PHP特區 : [分享]格式化數字字串:number_format()

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[分享]格式化數字字串:number_format()
函數:number_format()
功能:格式化數字字串。
語法: string number_format(float number, int [decimals], string [dec_point], string [thousands_sep]);
返回值: 字串
函數種類: 數學運算

內容說明:
本函數用來將浮點參數 number 格式化。
若沒加參數 decimals 則返回的字串只留整數部份,加了此參數才依參數指定的小數點位數返回。
參數 dec_point 表示小數點的表示方式方法,預設值是 ".",若需要轉換成其它的小數點就可以在這個參數改掉。
參數 thousands_sep 為整數部份每三位的分隔符號(千分號),預設是 ","。
本函數最特別的地方就是參數數目,最少要有一個,也就是欲格式化的字串;
也可以有二個或者四個參數,但不能用三個參數。

注意:指定小數點的位數之後的數字直接捨棄,沒有四捨五入的情形。

使用範例:
<?
$short_pi = "3.14159";
$my_pi = number_format($short_pi, 2);
echo $my_pi."\n";   // 3.14
$foo = 850017.9021;
$new_foo = number_format($foo, 3, ".", " ");
echo $new_foo."\n";  // 850 017.902
?>
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[分享]PHP number_format() 函数

PHP number_format() 函数

实例

格式化数字:



<?php
echo number_format("5000000")."<br>";
echo number_format("5000000",2)."<br>";
echo number_format("5000000",2,",",".");
?>

运行实例

定义和用法

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

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

语法

number_format(number,decimals,decimalpoint,separator)
参数描述
number

必需。要格式化的数字。

如果未设置其他参数,则数字会被格式化为不带小数点且以逗号(,)作为千位分隔符。

decimals可选。规定多少个小数。如果设置了该参数,则使用点号(.)作为小数点来格式化数字。
decimalpoint可选。规定用作小数点的字符串。
separator

可选。规定用作千位分隔符的字符串。仅使用该参数的第一个字符。比如 "xxx" 仅输出 "x"。

注释:如果设置了该参数,那么所有其他参数都是必需的。

技术细节

返回值:返回已格式化的数字。
PHP 版本:4+
更新日志:

自 PHP 5.4 起,该函数在参数 decimalpointseparator 中支持多字节。

在更老的版本中,只使用每个分隔符的第一个字节。

更多实例

例子 1

您希望返回一个价格:一个参数将对数字进行舍入(格式化为不带小数位),两个参数给出您想要的结果:



<?php
$num = 4999.9;
$formattedNum = number_format($num)."<br>";
echo $formattedNum;
$formattedNum = number_format($num, 2);
echo $formattedNum;
?>

运行实例


原文出處:PHP number_format() 函数
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15766
[分享]number_format

number_format

(PHP 4, PHP 5, PHP 7)

number_formatFormat a number with grouped thousands

Description

string number_format ( float $number [, int $decimals = 0 ] )
string number_format ( float $number ,
int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )

This function accepts either one, two, or four parameters (not three):

If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.

If two parameters are given, number will be formatted with decimals
decimals with a dot (".") in front, and a comma (",") between every group of thousands.

If all four parameters are given, number will be formatted with decimals decimals, dec_point instead of a dot (".") before the decimals and thousands_sep instead of a comma (",") between every group of thousands.

Parameters

number

The number being formatted.

decimals

Sets the number of decimal points.

dec_point

Sets the separator for the decimal point.


thousands_sep

Sets the thousands separator.

Return Values

A formatted version of number.

Changelog

VersionDescription
5.4.0 This function now supports multiple bytes in dec_point and thousands_sep. Only the first byte of each separator was used in older versions.

Examples


Example #1 number_format() Example

For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. This is achieved with this line :

<?php

$number 
1234.56 ;

// english notation (default)
$english_format_number  number_format ( $number
);
// 1,235

// French notation
$nombre_format_francais  number_format ( $number 2 ',' ' ' );
// 1 234,56

$number  1234.5678 ;


// english notation without thousands separator
$english_format_number  number_format ( $number 2 '.' '' );
// 1234.57

?>


原文出處:PHP: number_format - Manual
前一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|