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

青灯夜游
青灯夜游 原创
2023-03-15 14:36:01 1879浏览

转换方法: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视频教程

以上就是php怎么将int(整数)类型转为小数的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。