doupo1908 2014-05-23 11:07
浏览 28
已采纳

如何将包含€的$变量乘以一个值

since I'm new to PHP I have a quite simple question. After Googling and searching here on stackoverflow I still can't get it to work.

<?php 
    $payinfront = $product->get_price_html();
    $totalprice = $payinfront * 2;
?>

<p class="price">Total price: <?php echo $totalprice ?></p>
<p class="price">Amount to pay in front: <?php echo $payinfront ?></p>
<p class="price">Amount to pay after: <?php echo $totalprice - $payinfront ?></p>

The $payinfront value does get it's value from another part of my template. Let's say it's €10,-. This is the amount people have to pay in front. When we have done the service they have to pay the other half wich is the last rule.

Thanks for helping me out!

  • 写回答

4条回答 默认 最新

  • dpd20130 2014-05-23 11:18
    关注

    You want to store prices as floats, not as strings, so that PHP can recognize them as numeric values, and do calculations on them. Only cast them to strings at the very last moment, when you are echoing them in your template.

    <?php
        $payInFront = $product->getPrice(); // should return a numeric value, instead of a string
        $totalPrice = $payInFront * 2;
        $payAfter   = $totalPrice - $payInFront;
    ?>
    

    Then, when you echo the prices, you might want to format them in a certain way, using number_format():

    <p>Price: &euro; <?php echo number_format($totalPrice, 2, ',', '.'); ?></p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法