国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 php中的sprintf的應(yīng)用

php中的sprintf的應(yīng)用

Jun 08, 2016 pm 05:32 PM
format Money quot

<script>ec(2);</script>

sprintf

將字串格式化。

語法: string sprintf(string format, mixed [args]...);

傳回值: 字串

函式種類: 資料處理

內(nèi)容說明

本函式用來將字串格式化。參數(shù) format 是轉(zhuǎn)換的格式,以百分比符號 % 開始到轉(zhuǎn)換字符為止。而在轉(zhuǎn)換的格式間依序包括了

1. 填空字元。0 的話表示空格填 0;空格是內(nèi)定值,表示空格就放著。

2. 對齊方式。內(nèi)定值為向右對齊,負號表向左對齊。

3. 欄位寬度。為最小寬度。

4. 精確度。指在小數(shù)點后的浮點數(shù)位數(shù)。

型態(tài),見下表

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
轉(zhuǎn)換字符
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% 印出百分比符號,不轉(zhuǎn)換。
b 整數(shù)轉(zhuǎn)成二進位。
c 整數(shù)轉(zhuǎn)成對應(yīng)的 ASCII 字元。
d 整數(shù)轉(zhuǎn)成十進位。
f 倍精確度數(shù)字轉(zhuǎn)成浮點數(shù)。
o 整數(shù)轉(zhuǎn)成八進位。
s 整數(shù)轉(zhuǎn)成字串。
x 整數(shù)轉(zhuǎn)成小寫十六進位。
X 整數(shù)轉(zhuǎn)成大寫十六進位。

例子

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

使用范例


$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// 此時變數(shù) $money 值為 "123.1";
$formatted = sprintf ("%01.2f", $money);
// 此時變數(shù) $ formatted 值為 "123.10"
?>

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

這個 %01.2f 是什么意思呢?

首先 這個 % 符號是開始的意思,他寫在最前面表示指定格式要開始了。 也就是 "起始字符", 直到出現(xiàn) "轉(zhuǎn)換字符" 為止,就算格式終止。

然后 跟在 % 符號 后面的是 0 這個零是 "填空字元" 表示,如果位置空著 就用0來填滿。
在 0 后面的是 1 這個 1 是規(guī)定,小數(shù)點前面的數(shù)字占位要有1位以上。

如果把 1 改成 2 如果 $money 的值為 1.23 ,則 $formatted 的值將為 01.23
因為,在小數(shù)點前面的數(shù)字只占了1位,按照上面所規(guī)定的格式,小數(shù)點前數(shù)字應(yīng)該占2位,現(xiàn)在只有1位,所以,用0來填滿。

到目前,在 %01 后面的 .2 (點2) 就很好理解了,它的意思是,規(guī)定,小數(shù)點后的數(shù)字,必需占2位. 如果這時候,$money 的值為 1.234,則 $formatted 的值將為 1.23。
為什么 4 不見了呢? 因為,在小數(shù)點后面 按照上面的規(guī)定,必需且僅能占2位。 可是 $money 的值中,小數(shù)點占了3位,所以,4 被去掉了,只剩下 23。

最后,以 f "轉(zhuǎn)換字符" 結(jié)尾,其他轉(zhuǎn)換字符請自行參考上面的轉(zhuǎn)換字符列表。

關(guān)于對齊

如果在 % 起始符號后面 加上 - (負號) 則,將會把數(shù)字以向右對齊的方式進行處理。

列子



$money = 1.4;

$formatted = sprintf ("%-02.2f", $money);

echo $formatted;
?>

這時候,$formatted 將不會再是 01.40 而是 1.400

?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

php提交表單通過后,彈出的對話框怎樣在當前頁彈出,該如何解決 php提交表單通過后,彈出的對話框怎樣在當前頁彈出,該如何解決 Jun 13, 2016 am 10:23 AM

php提交表單通過后,彈出的對話框怎樣在當前頁彈出php提交表單通過后,彈出的對話框怎樣在當前頁彈出而不是在空白頁彈出?想實現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗證用PHP在后端,那么就用Ajax;僅供參考:HTML code

What is the usage of format in python What is the usage of format in python Jul 31, 2023 pm 01:59 PM

The usage of format in python is basic usage, specifying location, specifying variable name, formatting numbers, formatting date and time.

PHP formats a GMT/UTC date/time PHP formats a GMT/UTC date/time Mar 21, 2024 am 10:41 AM

This article will explain in detail how to format a GMT/UTC date/time with PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Introduction to Formatting GMT/UTC Date/Time in PHP In PHP, formatting GMT/UTC date/time is crucial for correctly displaying and handling cross-time zone dates. This article will explain how to format a GMT/UTC date/time using PHP's DateTime class, as well as the various formatting options available. DateTime class The DateTime class represents a date and time. It can store and manipulate date/time values ??in time zones such as GMT/UTC. To create a new Da

What does format mean in python What does format mean in python Jul 31, 2023 pm 02:05 PM

In Python, `format` is a built-in function used to format strings. It is used to create a string template with placeholders and fill the placeholders with specified values. This allows strings to be constructed dynamically based on different situations, making the output more readable and customizable.

How to convert a string into formatted date and time in MySQL How to convert a string into formatted date and time in MySQL Jun 01, 2023 pm 09:22 PM

STR_TO_DATE(date, format): Convert the string into the date and time in format SELECTSTR_TO_DATE(‘2015-01-01’,‘%Y-%m-%d’)->2015-01-01

What does format in python mean? What does format in python mean? Jul 31, 2023 pm 02:02 PM

format in Python is a string formatting method used to insert variables or values ??into placeholder positions in a string. Through the format method, we can dynamically construct a string to contain different values.

圖片消失怎么解決 圖片消失怎么解決 Apr 07, 2024 pm 03:02 PM

圖片消失如何解決先是圖片文件上傳$file=$_FILES['userfile']; ?if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

String formatting in Python: How to use the format() function String formatting in Python: How to use the format() function Apr 22, 2023 pm 07:01 PM

Method of inserting variables into a string The format() function in Python is a method of inserting variables into a string, which can make the string easier to read and understand. It supports many different usages. The following are specific usages and instructions: Use positional parameters to pass variables name='John'age=25print('Mynameis{},andIam{}yearsold.'.format(name,age))#output :MynameisJohn,andIam25yearsold. Use the index to pass the variable name='

See all articles