微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 将int转换为float / double

参见英文答案 > 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

原文地址:https://www.jb51.cc/php/132508.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐