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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...