转换方法:1、使用number_format()函数,语法“number_format(int类型的数值,小数位数)”;2、使用sprintf()函数,语法“sprintf("%.xf",int类型的值)”,参数x为小数位数。

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

php将int(整数)类型转为小数

方法1:使用number_format()函数

number_format()可通过千位分组来格式化数字。

<?php
echo number_format(12,1)."<br>";
echo number_format(12,2)."<br>";
echo number_format(12,3)."<br>";
echo number_format(12,4)."<br>";
?>

1.png

方法2:使用sprintf()函数

sprintf() 函数把格式化的字符串写入一个变量中。

<?php
echo sprintf("%.1f",12)."<br>";
echo sprintf("%.2f",13)."<br>";
echo sprintf("%.3f",14)."<br>";
echo sprintf("%.4f",15)."<br>";
?>

2.png


php怎么将int(整数)类型转为小数