首頁 後端開發 php教程 php number_format函數怎麼用?

php number_format函數怎麼用?

Apr 17, 2019 am 10:59 AM
number_format php

number_format函數是PHP中的內建函數,用於格式化一個包含數千個分組的數字,其語法是「string number_format ( $number, $decimals, $decimalpoint, $sep )」。

php number_format函數怎麼用?

這篇文章主要跟大家介紹php number_format用法,希望對需要的朋友有幫助!

number_format()函數是PHP中的內建函數,用來格式化一個包含數千個分組的數字。它在成功時會傳回格式化的數字,否則在失敗時給出E_WARNING

語法:

string number_format ( $number, $decimals, $decimalpoint, $sep )

參數:

此函數接受上述四個參數,如下所述:

$number:它是指定要格式化的數字的必要參數。如果沒有設定其他參數,則該數字將被格式化為不含小數,並使用逗號(,)作為千位分隔符號。必須參數指定要格式化的數字。

$decimals:它是可選參數,用來指定小數。如果設定了此參數,則該數字將使用圓點(.)作為小數點進行格式化。

$decimalpoint:它是一個可選參數,用來指定要用於小數點的字串。

$sep:它是可選參數,用於指定用於千位分隔符號的字串。如果給定了該參數,則需要所有其他參數。

傳回值:

成功時傳回格式化的數字,失敗時傳回E_WARNING。

number_format()程式碼範例1:

<?php 
$num1 = "999999.49"; 
  
// 没有小数点参数 
echo number_format($num1)."\n"; 
  
// 带小数点参数 
echo number_format($num1, 3)."\n"; 
   
$num2 = "9999999.99"; 
  
// 没有小数点参数
//返回整数值
echo number_format($num2)."\n"; 
  
//带小数点参数
echo number_format($num2, 3)."\n";  
  
// 包含所有四个参数
echo number_format("1000000.99", 3, "#", "GGG"); 
  
   
?>

#輸出:

999,999
999,999.490
10,000,000
9,999,999.990
1GGG000GGG000#990

number_format()程式碼範例2:

如果傳遞任何東西而不是數字,它則會給予警告。

<?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:

此函數不接受三個參數,只接受1、 2或4個參數。

<?php 
$num = 1000000; 
  
echo number_format($num, 3, ", "); 
?>

輸出:

PHP Warning:  Wrong parameter count for number_format() 
in /home/e426108b066d9a86366249bf7b626d19.php on line 6

相關推薦:《PHP教學

以上是php number_format函數怎麼用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP教程
1596
276
Edge PDF查看器不起作用 Edge PDF查看器不起作用 Aug 07, 2025 pm 04:36 PM

testthepdfinanotherapptoderineiftheissueiswiththefileoredge.2.enablethebuilt inpdfviewerbyTurningOff“ eflblyopenpenpenpenpenpdffilesexternally”和“ downloadpdffiles” inedgesettings.3.clearbrowsingdatainclorwearbrowsingdataincludingcookiesandcachedcachedfileresteroresoreloresorelorsolesoresolesoresolvereresoreorsolvereresoreolversorelesoresolvererverenn

VS代碼快捷方式專注於Explorer面板 VS代碼快捷方式專注於Explorer面板 Aug 08, 2025 am 04:00 AM

VSCode中可通過快捷鍵快速切換面板與編輯區。要跳轉至左側資源管理器面板,使用Ctrl Shift E(Windows/Linux)或Cmd Shift E(Mac);返回編輯區可用Ctrl `或Esc或Ctrl 1~9。相比鼠標操作,鍵盤快捷鍵更高效且不打斷編碼節奏。其他技巧包括:Ctrl KCtrl E聚焦搜索框,F2重命名文件,Delete刪除文件,Enter打開文件,方向鍵展開/收起文件夾。

修復:Windows Update無法安裝 修復:Windows Update無法安裝 Aug 08, 2025 pm 04:16 PM

runthewindowsupdatetrubloubleshooterviaSettings>更新&安全> is esseShootsoAtomationfixCommonissues.2.ResetWindowSupDateComponentsByStoppingRealatedServices,RenamingTheSoftWaredWaredWaredSoftwaredSistribution andCatroot2Folders,intrestrestartingthertingthertingtherserviceSteStoceTocle

比較和對比PHP特徵,抽像類別和界面與實際用例。 比較和對比PHP特徵,抽像類別和界面與實際用例。 Aug 11, 2025 pm 11:17 PM

Useinterfacestodefinecontractsforunrelatedclasses,ensuringtheyimplementspecificmethods;2.Useabstractclassestosharecommonlogicamongrelatedclasseswhileenforcinginheritance;3.Usetraitstoreuseutilitycodeacrossunrelatedclasseswithoutinheritance,promotingD

如何使用PHP中的陣列 如何使用PHP中的陣列 Aug 20, 2025 pm 07:01 PM

phparrayshandledatAcollectionsefefityIndexedorassociativuctures; hearecreatedWithArray()或[],訪問decessedviakeys,modifybyAssignment,iteratifybyAssign,iteratedwithforeach,andManipulationUsfunsionsFunctionsLikeCountLikeCountLikeCountLikeCountLikecount()

掌握foreach內部使用休息,繼續和goto的流量控制 掌握foreach內部使用休息,繼續和goto的流量控制 Aug 06, 2025 pm 02:14 PM

breakexitstheloopimmediatelyafterfindingatarget,idealforstoppingatthefirstmatch.2.continueskipsthecurrentiteration,usefulforfilteringitemsliketemporaryfiles.3.gotojumpstoalabeledstatement,acceptableinrarecaseslikecleanuporerrorhandlingbutshouldbeused

修復:以太網'身份不明網絡” 修復:以太網'身份不明網絡” Aug 12, 2025 pm 01:53 PM

Restartyourrouterandcomputertoresolvetemporaryglitches.2.RuntheNetworkTroubleshooterviathesystemtraytoautomaticallyfixcommonissues.3.RenewtheIPaddressusingCommandPromptasadministratorbyrunningipconfig/release,ipconfig/renew,netshwinsockreset,andnetsh

描述觀察者的設計模式及其在PHP中的實現。 描述觀察者的設計模式及其在PHP中的實現。 Aug 15, 2025 pm 01:54 PM

TheObserverdesignpatternenablesautomaticnotificationofdependentobjectswhenasubject'sstatechanges.1)Itdefinesaone-to-manydependencybetweenobjects;2)Thesubjectmaintainsalistofobserversandnotifiesthemviaacommoninterface;3)Observersimplementanupdatemetho

See all articles