【问题标题】:Convert string to integer and multiply two integers将字符串转换为整数并将两个整数相乘
【发布时间】:2012-09-16 19:21:15
【问题描述】:

我经常尝试将字符串转换为整数或将两个整数相乘。我无法将字符串转换为整数,因为它导致我变成布尔值(当我使用 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

    ?>

我尝试将支付费用转换为整数,但仍然无法正常工作。我以前做过这样的事情并且效果很好,但这次不行。

任何帮助将不胜感激。

P.S 感谢整个 stackoverflow.com 社区,他们之前曾多次帮助过我。

【问题讨论】:

  • 有没有办法得到纯数字的总数,没有格式化?如果是这样:疯狂!
  • @deceze 有什么想法吗?我以前做过这样的事情并且没有问题。为什么我需要格式化?我尝试格式化,但也许我做错了......
  • 我不知道。那是什么推车系统?如果是第三方系统,请阅读有关如何获取原始数字的文档。否则,我们需要查看您正在使用的确切数据。
  • @demlasjr 我之前关于注册用户数的回复 - 但是 - 查看 WooCommerce API,我认为您想要的是 $paypal = $commerce-&gt;cart-&gt;total * 0.025as the $total value in their class is a float and not a string
  • 除了@h2o 的建议之外,在我看来,这应该集成到支付处理器类之一中,并且可能已经集成到 Paypal 组件中。我没有深入研究它以提供更多细节。

标签: php string int woocommerce


【解决方案1】:

OP 正在运行 WooCommerce,他的 $commerce-&gt;cart-&gt;get_total(); 函数响应输出,例如&lt;span class="amount"&gt;560&amp;nbsp;&amp;euro;&lt;/span&gt;(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);
?>

但是,根据Woo Commerce API Documentation,您应该能够使用$commerce-&gt;cart-&gt;total 来获取数字的浮点数,因此可能的解决方案也可以工作(同样,我对 WooCommerce 一无所知),将是以下内容:

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

    $totalAmountWithFee = $totalAmountFloat + $feeForThisAmount;
    var_dump($totalAmountWithFee);
?>

编辑

根据你最新的数据转储,问题是你正在使用

$paypal_fees=$woocommerce->cart->get_total() * 0.025;

你应该在哪里使用

$paypal_fees=$woocommerce->cart->total * 0.025;

-&gt;get_total() 接收一个字符串,-&gt;total 接收一个浮点数。

【讨论】:

  • 本网站收到的最佳答案!然而,做编辑部分,我仍然得到一个 NULL,做第一部分, var_dump($feeForThisAmount);给了我 float(14),这是正确的!我想我需要给你送一辆装满啤酒的卡车。
  • @demlasjr 感谢您的好话。如果你var_dump($woocommerce-&gt;cart-&gt;total),你会得到什么?还有NULL?
  • 不客气。 var_dump($woocommerce->cart->total) 给出 string(6) "560.00" ////////// 看起来像 $paypal_fees=$woocommerce->cart->total * 0.025;给我浮动(14)。在给我NULL之前,可能我在那里犯了一个错误。对此感到抱歉 ////////// 但是,第一个选项效果很好
  • 这是因为,正如var_dump 所证明的那样,它不是一个浮点数,而是一个字符串(尽管文档中的说法不同)。所以你应该可以使用$paypal_fees=(float)$woocommerce-&gt;cart-&gt;total * 0.025;
  • 我想我之前犯了一个错误,因为现在再试一次并且在这两种方式上都可以完美运行!你帮了我很多这一切!您应该开始为 woocommerce 制作缺少的插件,并将它们发布到 woocommerce 市场和 codecanyon。人们和我很乐意购买它们!
【解决方案2】:

试试这个

$integer =(int)$string;

LIve example

var_dump() 正确

check this link

【讨论】:

  • 能否像在 C++ 中一样使用 int($string)
  • @MarkGarcia 不,你可以这样使用它会导致Parse error: syntax error, unexpected T_VARIABLE
  • string(31) "560" - 什么?听起来那里有不可见的字符(考虑到 31 个字符的长度)。试试var_dump(urlencode($string));
  • @demlasjr 所以它实际上是在一个 HTML 框中(即一个跨度),如果您查看源代码,您将能够看到它(但是它没有在站点,因为它只输出 HTML)。尝试调用var_dump(strip_tags($string));先删除HTML标签
  • @demlasjr 你已经在那里了!这意味着你可以说$integer = (int)strip_tags($string);,你就会得到你的号码。
【解决方案3】:

像使用类型转换

$integer = (int)$myString;

然后你可以将它转换为整数,并且它变得容易相乘

【讨论】:

  • 您的答案与@RegisteredUser 有何不同
【解决方案4】:

使用 intval() 函数将字符串转换为整数

intval

【讨论】:

  • 如果我这样做,我在 var_dump 中得到的只是“int(0)”:)
【解决方案5】:

从您的小学技术算法发布here。在这种情况下,您不需要将字符串转换为整数

【讨论】:

    猜你喜欢
    • 2012-05-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2010-12-31
    相关资源
    最近更新 更多