转换方法:1、使用number_format()函数,语法“number_format(int类型的数值,小数位数)”;2、使用sprintf()函数,语法“sprintf(%.xf,int类型的值)”,参数x为小数位数。
本教程操作环境:windows7系统、php7.1版、dell g3电脑
php将int(整数)类型转为小数
方法1:使用number_format()函数
number_format()可通过千位分组来格式化数字。
<?phpecho number_format(12,1)."<br>";echo number_format(12,2)."<br>";echo number_format(12,3)."<br>";echo number_format(12,4)."<br>";?>
方法2:使用sprintf()函数
sprintf() 函数把格式化的字符串写入一个变量中。
<?phpecho sprintf("%.1f",12)."<br>";echo sprintf("%.2f",13)."<br>";echo sprintf("%.3f",14)."<br>";echo sprintf("%.4f",15)."<br>";?>
推荐学习:《php视频教程》
以上就是php怎么将int(整数)类型转为小数的详细内容。