具有百分比的PHP数学方程

时间:2017-02-06 20:46:28

标签: php math

我怎么写这个等式。

这就是我所拥有的

(float) $total = (float) $product_price * (float) $percentage;
return (float) $total;

虽然现在我需要添加一个国家/地区百分比。因此,如果国家百分比为10%,我需要将10%加到当前总数上并将其返回。

我使用变量$ country_percentage

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您想要将百分比(税?)添加到总数中。如果$country_percentage是整数'10',则等于'0.10'

($total * (1 + ($country_percentage / 100))

哪个是($total * 1.10)

答案 1 :(得分:0)

你使用数学,我的朋友。如果您的百分比是字符串,请使用str_replace()删除%符号,然后使用intval($int)floatval($float)解析该值。这将为您提供一个号码,例如10

然后您将国家/地区百分比除以100

10/100 = 0.1

最后,您将产品价格乘以上述值+1。

所以:

//if $percentage is a string with '%' in it
$percentage = (floatval( str_replace("%", $percentage) );

$percentageNum = ($percentage/100) + 1;

$total = $product_price * $percentageNum;