如何将包含 € 的 $variable 乘以一个值 [英] How to multiply $variable that includes € with a value

查看:22
本文介绍了如何将包含 € 的 $variable 乘以一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是 PHP 新手,所以我有一个非常简单的问题.在谷歌搜索并在 stackoverflow 上搜索后,我仍然无法让它工作.

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>

$payinfront 值确实从我模板的另一部分获取它的值.假设它是 10 欧元,-.这是人们必须在前面支付的金额.当我们完成服务时,他们必须支付另一半,这是最后一条规则.

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.

谢谢你帮助我!

推荐答案

您想将价格存储为 floats,而不是字符串,以便 PHP 可以将它们识别为数值,并对其进行计算.仅在最后一刻将它们转换为字符串,当您在模板中 echo 使用它们时.

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;
?>

然后,当您回显价格时,您可能希望以某种方式格式化它们,使用number_format():

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>

这篇关于如何将包含 € 的 $variable 乘以一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆