<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>运达&#039;s  blog &#187; 数字</title>
	<atom:link href="https://www.yunda51.com/?feed=rss2&#038;tag=%E6%95%B0%E5%AD%97" rel="self" type="application/rss+xml" />
	<link>https://www.yunda51.com</link>
	<description>运达的博客</description>
	<lastBuildDate>Wed, 12 Nov 2025 07:58:26 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0.19</generator>
	<item>
		<title>PHP实现数字补零功能的2个函数介绍</title>
		<link>https://www.yunda51.com/?p=1699</link>
		<comments>https://www.yunda51.com/?p=1699#comments</comments>
		<pubDate>Fri, 15 Apr 2016 01:57:18 +0000</pubDate>
		<dc:creator><![CDATA[运达]]></dc:creator>
				<category><![CDATA[php技术]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[数字]]></category>
		<category><![CDATA[补零]]></category>

		<guid isPermaLink="false">http://www.yunda51.com/?p=1699</guid>
		<description><![CDATA[在PHP中至少有两个函数能够帮助我们快速实现数字补零： 首先是PHP str_pad函数： 代码如下: #st<a href="https://www.yunda51.com/?p=1699" class="read-more">Continue Reading</a>]]></description>
				<content:encoded><![CDATA[<p>在PHP中至少有两个函数能够帮助我们快速实现数字补零：<br />
首先是PHP str_pad函数：</p>
<pre class="wp-code-highlight prettyprint">
代码如下:
#str_pad — 使用另一个字符串填充字符串为指定长度
</pre>
<p>顾名思义这个函数是针对字符串，对指定的字符串填补任何其它的字符串<br />
str_pad参数说明：</p>
<pre class="wp-code-highlight prettyprint">
代码如下:
string str_pad ( string $input , int $pad_length [, string $pad_string = &quot; &quot; [, int $pad_type = STR_PAD_RIGHT ]] )
#常用参数说明：str_pad(带填补的字符串,填补后的长度，填补字符串，填补位置)
</pre>
<p>其中填补后的长度必须是个正整数，填补位置有三个选项，<br />
左边：STR_PAD_LEFT<br />
右边：STR_PAD_RIGHT<br />
两端：STR_PAD_BOTH<br />
实例展示：</p>
<pre class="wp-code-highlight prettyprint">
代码如下:
echo str_pad(1,8,&quot;0&quot;,STR_PAD_LEFT);
#结果：00000001
echo str_pad(1,8,&quot;0&quot;,STR_PAD_RIGHT);
#结果：10000000
echo str_pad(1,8,&quot;0&quot;,STR_PAD_BOTH);
#结果：00010000
$pad=str_pad(($number[0][&quot;count(*)&quot;]+1),4,&quot;0&quot;,STR_PAD_LEFT);  //自动补零
</pre>
<p>在上边的例子中值得注意的一个细节是，如果填补的位数是个奇数，例如例三中填补了7个0，右边优先。</p>
<p>下面再看下补零的另外一种方法：<br />
PHP sprintf函数：</p>
<pre class="wp-code-highlight prettyprint">
代码如下:
#sprintf — 返回一个格式化字符串
</pre>
<p>这个函数用起来比较灵活，有待学者们深挖，这里主要讲实现数值左边补零（或者在小数点后补零）的处理方式；<br />
先看左边补零</p>
<pre class="wp-code-highlight prettyprint">
代码如下:
echo sprintf(&quot;%05d&quot;,1);
# %05d的意思：用一个5位数的数字格式化后边的参数，如果不足5位就补零
# 运行结果是00001
</pre>
<p>再看小数点后补零</p>
<pre class="wp-code-highlight prettyprint">
代码如下:
echo sprintf(&quot;%01.3f&quot;,1);
# %01.3f的意思：用一个小数点后最少三位不足三位补零，小数点前最少一位，不足一位补零的浮点数格式化后边的参数
# 运行结果是：1.000
</pre>
<p>另外还可以自己编写一个自定义函数进行处理；<br />
编写代码各有所优也有所劣，大家可以任选适合的；</p>
<p>注：sprintf能够保证不至于误操作把1补成1000000，str_pad可以保证想补啥补啥。</p>
<p>转载请注明转自:<a href="http://www.yunda51.com">运达's blog</a> 原文地址：<a href="http://www.yunda51.com/1699.html">http://www.yunda51.com/1699.html</a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.yunda51.com/?feed=rss2&#038;p=1699</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
