【发布时间】:2020-07-12 11:55:26
【问题描述】:
我在下面使用 php 进行了一个简单的计算,如果数字是正数,我想得到用“+”显示的总和,例如。 '+121',可以吗?
$total = $row["subtotal1"] - $row["subtotal2"];
echo ".$total."
【问题讨论】:
标签: php
我在下面使用 php 进行了一个简单的计算,如果数字是正数,我想得到用“+”显示的总和,例如。 '+121',可以吗?
$total = $row["subtotal1"] - $row["subtotal2"];
echo ".$total."
【问题讨论】:
标签: php
如果你的总数大于0,你可以回显+符号:echo ($total > 0 ? '+' : '').$total;
$total = 2;
echo ($total > 0 ? '+' : '').$total; // +2
$total = 0;
echo ($total > 0 ? '+' : '').$total; // 0
$total = -1;
echo ($total > 0 ? '+' : '').$total; // -1
【讨论】:
你可以使用如下的辅助函数:
function getPositiveOrNegative($number){
return ($number >= 0) ? '+' : '';
}
$number = 10;
echo getPositiveOrNegative($number).$number.'<br/>';
$number = -20;
echo getPositiveOrNegative($number).$number.'<br/>';
$number = 0;
echo getPositiveOrNegative($number).$number.'<br/>';
输出:
+10
-20
+0
【讨论】: