如何从xml文件中提取值并进行数学运算? [英] How to extract values from xml file and do mathematical operations?

查看:36
本文介绍了如何从xml文件中提取值并进行数学运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个值与一个 xml 文件中的另一个值相乘,它可以工作,但不是我想要的.如果 $amount 是 5,当我乘以 $eurotodollar 时,我仍然得到 5 而不是 6.882.有什么我在这里遗漏的吗...

I want to multiply a value with another value from an xml file, it works but not as I want to. If the $amount is 5, when I multiply with $eurotodollar I still get 5 not 6.882. Is there something I am missing here...

我有 php 代码:

    <?php
    $xml = simplexml_load_file('curr.xml')or die("Error: Cannot create object");

    $eurtodollar = $xml->OrigCurrency->Cube->Rate[0];

    if(isset($_POST['submit']))
        {
        $amount = $_POST['amount'];
        $cur1 = $_POST['cur1'];
        if(!isset($_POST['cur2']))
        {
            echo "<center><b>Enter Values</b><br></center>";
        }else{
            $cur2 = $_POST['cur2'];
        }
        if($cur1=="EUR" AND $cur2=="USD")
        {
            echo "<center><b>Your Converted Amount is:</b><br></center>";
            echo "<center>" . $amount*$eurtodollar . "</center>";
        }
}
    ?>

xml 代码:

<body>
    <OrigCurrency val="Euro">
        <Cube date="2014-06-20">
            <Rate currency="USD">1.3764</Rate>
            <Rate currency="CHF">1.2079</Rate>
            <Rate currency="GBP">0.8731</Rate>
        </Cube>
    </OrigCurrency>
</body>

推荐答案

我认为使变量 float 可以解决您的问题.要使用 SimpleXMLElement 从变量中获取浮点值,您可以使用 floatval 函数:

I think making the variable float will fix your issue. To get a float value from the variable with the SimpleXMLElement, you can use the floatval function:

floatval($var);

您可以使用的 Fpr 示例

Fpr example you could use

$result = $amount * floatval($eurtodollar)

这篇关于如何从xml文件中提取值并进行数学运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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