加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 将int转换为float / double

发布时间:2020-12-13 16:36:04 所属栏目:PHP教程 来源:网络整理
导读:参见英文答案 PHP – Force integer conversion to float with three decimals1 当我想将整数值转换为float(带点的数字)时,我遇到麻烦. $a = 7200;$b = $a/3600;echo $b; // 2$b = floatval($b);echo $b; // 2 但应该回应2.0或2.00 我也试过settype,没有成功
参见英文答案 > PHP – Force integer conversion to float with three decimals1
当我想将整数值转换为float(带点的数字)时,我遇到麻烦.
$a = 7200;
$b = $a/3600;

echo $b; // 2

$b = floatval($b);

echo $b; // 2

但应该回应2.0或2.00

我也试过settype,没有成功.
我只找到“浮动到int”的帮助/解决方案/问题.

更新:

使用

echo sprintf("%.2f",$b); // returns 2.00

使用

echo number_format($b,2);

例如:

echo number_format(1234,2); // returns 1,234.00

编辑:

@DavidBaucum是的,number_format()返回字符串.

使用

echo sprintf("%.2f",$b);

对于你的问题,使用

Why number_format doesn’t work can be demonstrated by this. echo
number_format(1234,0) + 1.0 The result is 2

echo sprintf("%.2f",(1234 + 1.0 ) ); // returns 1235.00

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读