Leszek
Leszek

Reputation: 182

SimpleXMLElement value to int

I have problem with SimpleXMLElement. This is var_dump one of my SimpleXML position:

object(SimpleXMLElement)#42 (4) 
{ 
    ["nazwa_waluty"] => string(14) "rubel rosyjski" 
    ["przelicznik"]  => string(1)  "1" 
    ["kod_waluty"]   => string(3)  "RUB" 
    ["kurs_sredni"]  => string(6)  "0,0514" 
}

and I don't know how to get "kurs_sredni" as an intiger.
I tried (int) $position->kurs_sredni but it doesn't work.
Any suggestions?

EDIT: This is my method. I would like to get "kurs_sredni" as a float:

public function getExchangeRate($currency)
    {
        $filename = 'LastA.xml';
        $filepath = Mage::getBaseDir('base') . '/media/exchangerate/' . $filename;
        $exchangeRateXml = simplexml_load_file($filepath);

        foreach($exchangeRateXml->pozycja as $position){
            if ($position->kod_waluty == $currency) {
                var_dump((float) $position->kurs_sredni);die();
            }
        }
    }

and var_dump returns 0

Upvotes: 0

Views: 1887

Answers (2)

fusion3k
fusion3k

Reputation: 11689

The main problem with the string is that it is in european format, using comma as decimal separator instead of point.

So, you have first to replace , with .

$num = floatval( str_replace( ',','.', $position->kurs_sredni ) );

Upvotes: 1

MattOlivos
MattOlivos

Reputation: 229

Not sure of exactly all that you've tried because there's no posted code but from looking at your post you might have messed up:

$NewVariableName= (int) $StringVariableThatNeedsToBeChanged;
echo $NewVariableName; //This is the new variable as an integer

You need to create a new variable that's going to take place being a new integer value.

Upvotes: 1

Related Questions