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,没有成功.
更新:
使用 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); 对于你的问题,使用
echo sprintf("%.2f",(1234 + 1.0 ) ); // returns 1235.00 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |