将字符串转换为整数并乘以两个整数

dem*_*sjr 2 php string int woocommerce

我有很大的时间尝试将字符串转换为整数或乘以两个整数.我无法将字符串转换为整数,因为它导致我成为一个布尔值(当我使用var_dump时).我可以转换字符串中的其他整数,但我无法将其相乘.

我有这个:

    <? $fees=$commerce->cart->get_total(); 
    $payfee = str_replace('&nbsp;&euro;', '', $fees);
    $payfee = str_replace(',','', $payfee);  //this is the string
    $fee = 0.025;
    $paypal = $payfee * $fee;  //this thing is not working

    ?>
Run Code Online (Sandbox Code Playgroud)

我尝试将payfee转换为整数,但仍然无法使其工作.之前我做过类似的事情并且运作良好,但这次不是.

任何帮助将不胜感激.

PS感谢整个stackoverflow.com社区,它曾多次帮助过我.

h2o*_*ooo 5

OP正在运行WooCommerce,他的$commerce->cart->get_total(); 功能响应输出,例如<span class="amount">560&nbsp;&euro;</span>(560€),他正在询问如何将其转换为数字,以便他可以从金额中获得费用(2.5%).

首先,这里的问题是get_total()函数以字符串响应.

修复此字符串的正确方法是一个简单的例子,如

<?php
    $totalAmountString = $commerce->cart->get_total(); //<span class="amount">560&nbsp;&euro;</span>
    $totalAmountString = strip_tags($totalAmountString); //get rid of the span - we're left with "560&nbsp;&euro;"
    $totalAmountString = str_replace(array("&nbsp;&euro;", ","), "", $totalAmountString);
    $totalAmountFloat = (float)$totalAmountString;
    $fee = 0.025;
    $feeForThisAmount = $totalAmountFloat * $fee;
    var_dump($feeForThisAmount);

    $totalAmountWithFee = $totalAmountFloat + $feeForThisAmount;
    var_dump($totalAmountWithFee);
?>
Run Code Online (Sandbox Code Playgroud)

但是,根据Woo Commerce API文档,您应该能够使用$commerce->cart->total该数字的浮点数,因此可能也有效的解决方案(再次,我对WooCommerce一无所知)将是以下内容:

<?php
    $totalAmountFloat = $commerce->cart->total;
    $fee = 0.025;
    $feeForThisAmount = $totalAmountFloat * $fee;
    var_dump($feeForThisAmount);

    $totalAmountWithFee = $totalAmountFloat + $feeForThisAmount;
    var_dump($totalAmountWithFee);
?>
Run Code Online (Sandbox Code Playgroud)

编辑

根据您最新的数据转储,问题在于您正在使用

$paypal_fees=$woocommerce->cart->get_total() * 0.025;
Run Code Online (Sandbox Code Playgroud)

你应该在哪里使用

$paypal_fees=$woocommerce->cart->total * 0.025;
Run Code Online (Sandbox Code Playgroud)

as ->get_total()接收一个字符串,并->total接收一个浮点数.