K
K
kolomat2019-11-04 19:37:14
PHP
kolomat, 2019-11-04 19:37:14

Why is it wrong to multiply?

Good evening, the essence of the problem is this. It is necessary to parse the xml file and save it in yml, which is suitable for loading into the CMS.
The problem is the following in the source file there are currency tags in which the courses are registered.

<currency id="EUR" rate="27.5840"/>
<currency id="RUB" rate="0.3760"/>
<currency id="USD" rate="24.8500"/>

I write the values ​​of each currency into a separate variable like this
$eur = $xmlProducts->currency[0]->attributes()->rate;
$rub = $xmlProducts->currency[1]->attributes()->rate;
$usd = $xmlProducts->currency[2]->attributes()->rate;

Then, when processing an array with offers, I make such a comparison
$price =  $offer->appendChild($xml->createElement('price'));
  if($product->currencyId == 'USD'){
      $price = $price->appendChild($xml->createTextNode($product->price * $usd));

But for some reason, at the exit, the price turns out to be ~ 1-2% less than it should be. What could be the problem?
PS printout of $usd variable
SimpleXMLElement Object
(
    [0] => 24.8500
)

Offer printout
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [id] => 382
            [selling_type] => u
        )

    [name] => Дельфины коврик для крышки унитаза
    [categoryId] => 900064
    [price] => 4.43
    [currencyId] => USD
    [image] => Array
        (
            [0] => https://b2b.antey.com.ua/media/product_sku_000382_0.jpg
            [1] => https://b2b.antey.com.ua/media/product_sku_000382_1.jpg
        )

    [vendor] => KERAMAC
    [vendorCode] => SimpleXMLElement Object
        (
        )

    [param] => Array
        (
            [0] => Винил
            [1] => Другие цвета
            [2] => KERAMAC
            [3] => Flori
        )

    [description] => Дельфины коврик для крышки унитаза, винил
    [available] => true
)

But at the exit
<offer id="382">
        <name>Дельфины коврик для крышки унитаза</name>
        <categoryId>900064</categoryId>
        <price>96</price>
        <description>Дельфины коврик для крышки унитаза, винил</description>
        <vendorCode></vendorCode>
        <vendor>KERAMAC</vendor>
        <param>
          <param name="Материал">Винил</param>
          <param name="Цвет/тип покрытия">Другие цвета</param>
          <param name="Бренд">KERAMAC</param>
          <param name="Серия">Flori</param>
        </param>
      </offer>

That is, logically, as I suppose, it should be 4.43 * 24.8500 which is equal to 110, but in fact, as you can see, it turns out 96

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2019-11-04
@glaphire

There is not enough float precision, for exact monetary calculations there is moneyphp/money

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question