这篇文章给大家分享的是有关php小数精确几位的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
php小数精确几位的方法:1、利用sprintf()函数来格式化,语法“sprintf("%.小数位数f",$num)”;2、利用number_format()函数,语法“number_format($num,'小数位数')”。
本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑
php小数精确小数点后位数的方法
方法1:利用sprintf格式化字符串
<?php
header("Content-type:text/html;charset=utf-8");
$num = 10.4567;
$format_num = sprintf("%.2f",$num);
echo $format_num; //10.46
?>

方法2:利用number_format() 函数
number_format():函数可以通过千位分组的形式来格式化数字。
语法:
number_format(number,decimals,decimalpoint,separator)
参数:
示例:
<?php
header("Content-type:text/html;charset=utf-8");
$num = 10.4567;
$format_num = number_format($num,'1');
echo $format_num."<br>";
$format_num = number_format($num,'2');
echo $format_num."<br>";
$format_num = number_format($num,'3');
echo $format_num;
?>

感谢各位的阅读!关于“php小数精确几位的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!