如果没有小数部分,我想将 float 转换为 int。如果有小数部分,我希望最多为两位数。如何检查是否有小数部分?
所以我有这样的数据。
1.0000
2.0000
3.0000
9.8400
7.3200
8.0000
我想要这样的输出
1
2
3
9.84 round to two digits if there is decimal part
7.32
8
php中有没有内置函数可以知道 float 是否有小数部分?我如何在 php 中做到这一点?
最佳答案
我喜欢 printf
答案,但您可以检查该数字是否等于该数字的整数转换:
if( ($int = (int)$number) == $number) {
$number = $int;
} else {
$number = round($number, 2);
}
如果您确实想要 2 位小数,则使用 number_format
。
关于php - 如果没有小数部分,则将 float 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57186373/