日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

numberformate php_php number_format函数怎么用?

發(fā)布時(shí)間:2024/7/23 php 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 numberformate php_php number_format函数怎么用? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。