【问题标题】:How to calculate with php the total with a VAT percentage in PHP如何用 php 计算带有增值税百分比的 PHP 总额
【发布时间】:2015-01-02 04:41:00
【问题描述】:

我正在尝试使用以下值进行计算:

  • 产品成本(不含增值税)= 12,40 ($product)
  • 增值税百分比 = 21%,我将在数据库中存储为 0,21 ($vat_perc)
  • 增值税为 2,604 ($vat)

编辑:增值税是按产品计算的

当我尝试得到总数时,我得到 15,00 ($total)

我所做的是:

$total = $product + $vat 

这将回显 15.004

然后我使用number_format:

echo(number_format($total,2,',','.'));

这将打印 15.00

但是我想将乘积乘以 2

所以这将给出以下计算:

$total = $product * 2 + $vat 

然后我再次使用格式:

echo(number_format($total,2,',','.'));

那么总数 = 30,01

我尝试了一些类似 ROUND en INT 的方法,但没有成功。

我在这方面做错了什么?知道增值税在这里是邪恶的,但我不知道如何解决这个问题。

【问题讨论】:

  • $product * 2 + $vat 顺便说一句,这个公式正确吗?
  • @DavidThomas:正确,我改了标题,谢谢!
  • @zerkms 每个产品的增值税。
  • 在那个表达式中是每次购买

标签: php percentage


【解决方案1】:

$tax = round( ($price / 100) * 3.8, 2); 税是四舍五入的价格除以 100 以做出明确的百分比。乘以想要的堆栈 然后您在价格表中添加或从价格表中添加。

很高兴与您通电话 - 也许我们可以通过电话更快地解决这个问题。感谢上帝的电话!

干杯!

【讨论】:

    【解决方案2】:

    以下是一些如何使用 PHP 函数对数字进行四舍五入的示例。

    $product = 12.40;
    $vat = 2.644;
    $total = ( $product + $vat ) * 2;
    
    var_dump( $total );  // float(30.088)
    var_dump( number_format($total,2,',','.') );  // string(5) "30,09", rounded 
    var_dump( round( $total, 2 ) );  // float(30.09), rounded
    var_dump( sprintf('%0.2f', $total ) );  // string(5) "30.09", rounded
    var_dump( floor( $total * 100 ) / 100 );  //  float(30.08), not rounded
    

    所有三个函数(number_formatroundsprintf)都会对结果进行四舍五入,为了避免四舍五入并丢弃两个小数点后的小数部分,您可以使用最后一个示例。

    【讨论】:

      【解决方案3】:

      您的初始总数为 15.004,因此当您致电 number_format 时,将向下舍入为 15.00。现在,当您乘以 2 时,您的总数为 15.008,其中 number_format 将四舍五入为 15.01。问题不在于加法,而在于乘以 2。number_format 舍入到最近的位置,对于您的情况来说,它是 30.01。

      如果您希望数字一直向下舍入,请使用floor,如下所示:

      $total = floor(($product * 200)) / 100 + $vat;
      echo(number_format($total,2,',','.'));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-13
        • 2018-04-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多