PHP 數字前面補零 固定位數補0

2021-10-11 04:07:23 字數 443 閱讀 7549

str_pad(string,length,pad_string,pad_type)

//引數 描述

string //必需。規定要填充的字串。

length //必需。規定新的字串長度。如果該值小於字串的原始長度,則不進行任何操作。

pad_string //可選。規定供填充使用的字串。預設是空白。

pad_type //可選。規定填充字串的哪邊。

//可能的值:

str_pad_both - //填充字串的兩側。如果不是偶數,則右側獲得額外的填充。

str_pad_left - //填充字串的左側。

str_pad_right - //填充字串的右側。預設。

$num=128;

$num=str_pad($num,4,"0",str_pad_left);

echo $num;

PHP 數字前面補零 固定位數補0

在處理訂單編號的時候,需要固定位數的數字,比如需要固定四位數格式 1 0001 56 0056 288 0288 1992 1992可以使用php內建函式str pad 函式把字串填充為新的長度。str pad string,length,pad string,pad type 引數 描述 stri...

PHP PHP 數字前面補0滿足位數

需求 1 0001 56 0056 228 0228 2020 2020 可以使用php內建函式str pad 函式把字串填充為新的長度。str pad string,length,pad string,pad type 引數 描述 string 必需。規定要填充的字串。length 必需。規定新的...

php在數字前面補0得到固定長度數字的兩種方法

比較基礎,其實兩個內建函式都能實現。1 sprintf 語法 string sprintf string format,mixed args 返回值 字串 函式種類 資料處理 本函式用來將字串格式化。引數 format 是轉換的格式,以百分比符號 開始到轉換字元為止。而在轉換的格式間依序包括了 填空...