php小数精确几位的方法:1、利用sprintf()函数来格式化,语法“sprintf(%.小数位数f,$num)”;2、利用number_format()函数,语法“number_format($num,'小数位数')”。
本教程操作环境:windows7系统、php7.1版,dell g3电脑
php小数精确小数点后位数的方法
方法1:利用sprintf格式化字符串
<?phpheader("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)
参数:
number:必需。要格式化的数字。
decimals:可选。规定多少个小数
decimalpoint:可选。规定用作小数点的字符串。
separator:可选。规定用作千位分隔符的字符串。
示例:
<?phpheader("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视频教程》
以上就是php小数精确几位的方法有哪些的详细内容。