最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP自帶函數(shù)給數(shù)字或字符串自動補(bǔ)齊位數(shù)

 更新時間:2014年07月29日 09:00:23   投稿:hebedich  
很多時候我們需要對數(shù)字進(jìn)行格式化,比如位數(shù)不足前面加0補(bǔ)足。用 php可以很輕易實現(xiàn),因為PHP自帶了相關(guān)功能的函數(shù)。

先來看個例子:需求為生成4位數(shù),不足前面補(bǔ)0

<?php  
//生成4位數(shù),不足前面補(bǔ)0  
$var=sprintf("%04d", 2);
echo $var;//結(jié)果為0002  
echo date('Y_m_d', time()).'_'.sprintf('d', rand(0,99));
?>

sprintf()函數(shù)

有沒有感覺很像c語言

1. 語法

sprintf(format,arg1,arg2,arg++)
參數(shù) 描述
format 必需。轉(zhuǎn)換格式。
arg1 必需。規(guī)定插到 format 字符串中第一個 % 符號處的參數(shù)。
arg2 可選。規(guī)定插到 format 字符串中第二個 % 符號處的參數(shù)。
arg++ 可選。規(guī)定插到 format 字符串中第三、四等等 % 符號處的參數(shù)。

2. 說明

參數(shù) format 是轉(zhuǎn)換的格式,以百分比符號 ("%") 開始到轉(zhuǎn)換字符結(jié)束。下面的可能的 format 值:

%% - 返回百分比符號
%b - 二進(jìn)制數(shù)
%c - 依照 ASCII 值的字符
%d - 帶符號十進(jìn)制數(shù)
%e - 可續(xù)計數(shù)法(比如 1.5e+3)
%u - 無符號十進(jìn)制數(shù)
%f - 浮點數(shù)(local settings aware)
%F - 浮點數(shù)(not local settings aware)
%o - 八進(jìn)制數(shù)
%s - 字符串
%x - 十六進(jìn)制數(shù)(小寫字母)
%X - 十六進(jìn)制數(shù)(大寫字母)
arg1, arg2, ++ 等參數(shù)將插入到主字符串中的百分號 (%) 符號處。該函數(shù)是逐步執(zhí)行的。在第一個 % 符號中,插入 arg1,在第二個 % 符號處,插入 arg2,依此類推。

<?php  
$number = 123;  
$txt = sprintf("%f",$number);  
echo $txt;  
?>

3. 格式數(shù)字 number_format()

<?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 seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>

相關(guān)文章

最新評論

五华县| 德安县| 襄垣县| 新和县| 新闻| 手游| 广灵县| 儋州市| 塘沽区| 吴忠市| 尼木县| 尼木县| 玉环县| 垣曲县| 马山县| 辽中县| 德庆县| 砀山县| 南宁市| 江安县| 开化县| 灵宝市| 县级市| 莱西市| 石泉县| 玉环县| 通辽市| 元氏县| 阿勒泰市| 石门县| 定安县| 项城市| 东山县| 衡山县| 浦东新区| 阿巴嘎旗| 镇康县| 禄丰县| 资源县| 车致| 监利县|