numberformate php_php number_format函数怎么用?
number_format函數(shù)是PHP中的一個(gè)內(nèi)置函數(shù),用于格式化一個(gè)包含數(shù)千個(gè)分組的數(shù)字,其語(yǔ)法是“string number_format ( $number, $decimals, $decimalpoint, $sep )”。
本篇文章主要給大家介紹php number_format用法,希望對(duì)需要的朋友有所幫助!
number_format()函數(shù)是PHP中的一個(gè)內(nèi)置函數(shù),用于格式化一個(gè)包含數(shù)千個(gè)分組的數(shù)字。它在成功時(shí)返回格式化的數(shù)字,否則在失敗時(shí)給出E_WARNING。
語(yǔ)法:string number_format ( $number, $decimals, $decimalpoint, $sep )
參數(shù):
該函數(shù)接受上述四個(gè)參數(shù),如下所述:
$number:它是指定要格式化的數(shù)字的必需參數(shù)。如果沒(méi)有設(shè)置其他參數(shù),則該數(shù)字將被格式化為不帶小數(shù),并使用逗號(hào)(,)作為千位分隔符。必須參數(shù)指定要格式化的數(shù)字。
$decimals:它是可選參數(shù),用于指定小數(shù)。如果設(shè)置了此參數(shù),則該數(shù)字將使用圓點(diǎn)(.)作為小數(shù)點(diǎn)進(jìn)行格式化。
$decimalpoint:它是一個(gè)可選參數(shù),用于指定要用于小數(shù)點(diǎn)的字符串。
$sep:它是可選參數(shù),用于指定用于千位分隔符的字符串。如果給定了該參數(shù),則需要所有其他參數(shù)。
返回值:
成功時(shí)返回格式化的數(shù)字,失敗時(shí)返回E_WARNING。
number_format()代碼示例1:<?php
$num1 = "999999.49";
// 沒(méi)有小數(shù)點(diǎn)參數(shù)
echo number_format($num1)."\n";
// 帶小數(shù)點(diǎn)參數(shù)
echo number_format($num1, 3)."\n";
$num2 = "9999999.99";
// 沒(méi)有小數(shù)點(diǎn)參數(shù)
//返回整數(shù)值
echo number_format($num2)."\n";
//帶小數(shù)點(diǎn)參數(shù)
echo number_format($num2, 3)."\n";
// 包含所有四個(gè)參數(shù)
echo number_format("1000000.99", 3, "#", "GGG");
?>
輸出:999,999
999,999.490
10,000,000
9,999,999.990
1GGG000GGG000#990
number_format()代碼示例2:
如果傳遞任何東西而不是數(shù)字,它則會(huì)給出警告。<?php
$num = "GFG";
echo number_format($num)."\n\n";
echo number_format($num, 3);
?>
輸出:PHP Warning: number_format() expects parameter 1 to be float,
string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 5
PHP Warning: number_format() expects parameter 1 to be float,
string given in /home/ac476aaecea758334cb8ed146bcbb8f6.php on line 8
number_format()代碼示例3:
此函數(shù)不接受三個(gè)參數(shù),只接受1、2或4個(gè)參數(shù)。<?php
$num = 1000000;
echo number_format($num, 3, ", ");
?>
輸出:PHP Warning: Wrong parameter count for number_format()
in /home/e426108b066d9a86366249bf7b626d19.php on line 6
相關(guān)推薦:《PHP教程》
總結(jié)
以上是生活随笔為你收集整理的numberformate php_php number_format函数怎么用?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: html中属性的作用,html的标签中
- 下一篇: php下载文件与服务器有关吗,php 下