首页 > 解决方案 > PHP在做数学减法之前用前导点格式化千数?

问题描述

我正在尝试用千分之一的两个数字进行减法运算,其格式如下所示,但我无法弄清楚使用number_format()函数来获取thousand没有前导"dot"作为分隔符的显示,并按如下方式进行数学运算。

得到结果 0,032 而不是 32

<?php
$a = "1.545"; // how to format this to 1545? (here could be 0, 1, 6, 10, 54, 389, 600, 1600, 5000)
$b = "1.513";  // how to format this to 1545? (here could be 0, 1, 6, 10, 54, 389, 600, 1600, 5000)
$c = $a- $b; // subtraction
echo $c; // result 0,032 instad of just 32
?>

如何将 1.545 格式化为 1545 然后进行减法?

标签: phpmathinteger

解决方案


得到它的工作!结果 32

<?php
$a = "1.545";
$b = "1.513";
$c = intval(str_replace('.','',$a))-intval(str_replace('.','',$b));
echo $c;
?>

推荐阅读