php 的舍入函数在省略精度参数时,默认使用全局配置 precision 的值。要改变这个行为,需要调用 ini_set('precision', ...) 来修改当前脚本的精度设置,或者在调用函数时显式传入所需的整数精度。

round() 不传精度时的默认行为是什么
round() 函数在不指定第二个参数时,会读取 precision 配置项来决定保留几位小数。如果该配置为默认的 14,则返回结果会包含最多 14 位有效数字的小数部分,这通常会导致浮点数显示大量无意义的尾数。

$val = 1.234567890123456;
echo round($val); // 输出: 1
// 注意:round() 默认 precision 为 14,但 round 是四舍五入到指定位数
// 如果 precision 为 14,round(1.23...) 实际上会尝试保留 14 位小数
// 但 PHP 的 float 类型本身精度有限,这里主要看 precision 对输出的影响
// 实际上 round() 的 precision 参数默认为 ini_get('precision')</p>
<h2><code>number_format()</code> 不传精度时的默认值是多少</h2>
<p><code>number_format()</code> 函数在只传入一个参数(即数字)时,默认会将小数部分截断并格式化为 0 位小数,同时不会添加千位分隔符。这与 <code>round()</code> 依赖全局配置的行为不同,它是基于函数自身的默认参数定义。</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p>使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>
<p><img src="https://img.php.cn/upload/article/001/431/639/179651305078440.png" alt="PHP舍入函数没有传精度时怎么使用默认值"></p>
<pre class="brush:php;toolbar:false;">$num = 1234.5678;
echo number_format($num); // 输出: 1235
// number_format 默认 decimals=0, thousands_sep='', decimal_point='.'</p>
<h2>当 <code>precision</code> 被修改后,<code>round()</code> 的结果如何变化</h2>
<p>通过 <code>ini_set('precision', 5)</code> 将精度设置为 5 后,<code>round()</code> 函数将只保留 5 位小数。这意味着即使输入的数字有更多位数,输出也会被截断或四舍五入到第 5 位小数。这种变化是全局生效的,直到脚本结束或被重置。</p>
<pre class="brush:php;toolbar:false;">echo ini_get('precision'); // 默认通常是 14
ini_set('precision', 5);
$val = 1.23456789;
echo round($val); // 输出: 1.23457
// 此时 precision 为 5,所以 round 保留 5 位小数


















