Converting a price returned, with only one decimal place

Hey all,

Long time since i’ve posted asking for help on the forums!

I’ve been working on integrating the eBay API into a website I’m building, and all has been going relatively smoothly. Now I’m no PHP guru, i’ve only really gone feet first in the last couple of years, but I can work my way through the basics. However, I’m a bit stumped this one.

The eBay API returns item prices via XML REST as numbers such as:

£24.95
£24.99
£24.5

So basically when the price would normally end with a 0, it only goes to one decimal place.

The price has been pulled from the XML return and put into a variable so I can manipulate it “$currentPrice”.

Anyone have any advice on the best way to add the missing “0” to the returned price?

The important bit of code is below:


$resp = simplexml_load_file($apicall);


if ($resp->ack == "Success") {

  $results = '';
  $pagination = '';
  
  foreach($resp->searchResult->item as $item) {

    $searchresult = $resp->searchResult;
    $currentPrice = $item->sellingStatus->convertedCurrentPrice;
    $shippingCost = $item->shippingInfo->shippingServiceCost;


 

Cheers,

Ant

Do you mean it returns 24.5 or does it actually prepends the £ to make it £24.5?

First place I’d look is a setting in the API call check the documentation again, something which causes it to return 2 decimal places from now on, TBH.

Sorry, no real exp of PP API. Maybe someone else has better knowledge.

sprintf (see example 9) might help otherwise.

Sorry my bad, it doesn’t return the denomination,

So basically:


<currentPrice currencyId="GBP">200.0</currentPrice>
<convertedCurrentPrice currencyId="GBP">200.0</convertedCurrentPrice>

Is what is received back from the XML request.

Just tested out sprintf, Example 9, and it worked perfectly. Thanks for the suggestion Cups.

Only odd thing was as this is being built in ExpressionEngine, EE strips out the characters: %01 when you try to save the template, so you just end up with “.2f”.

Placing the “%01” in a snippet and then embedding in the template did the trick.

So, reading up on sprintf, and using the example I suggested from the man:


$price[] = "432.1";
$price[] = "321";
$price[] = "432.10";
$price[] = ".1";
$price[] = "0.10";
// add more test scenarios above if you want

foreach( $price as $p)
echo  sprintf("%01.2f", $p) . PHP_EOL ;

// gives:

432.10
321.00
432.10
0.10
0.10

EDIT
OK, sorry I posted before your reply, glad you got it sorted out quickly!

EDIT2
Creating an array of values you can test expectations against is a good trick, even better when asking questions if you prepare a set of test cases for us to test against, it doesn’t apply to every question asked of course, but its a good habit to get into :wink:

In light of edit2 above, run this lot through sprintf for a few surprises:


$price[] = "asdfasdfasdfasdf432.1";
$price[] = "321.asdfasdfasdf";
$price[] = "4.3.2.1.0";
$price[] = "ERROR";
$price[] = "x10";
$price[] = true;
$price[] = "<xml version=1>";

You have no idea what a 3rd party (Papal) might accidentally put into a field, so you should be prepared to defend against it all the same, probably just making sure the value is greater than, dunno - a pound, a penny?

Indeed, there’s no real control over eBay putting rubbish accidently into their XML returns, if they did I’d imagine it would screw up a few thousand people’s websites instantaneously :slight_smile:

I hadn’t really considered the need to filter results returned as maybe naively trust eBay’s API, but it’s a good point, even if just check it’s a valid numeric.

I agree it would be suicidal of PP to do that, I just got carried away with illustrating one way to test simple functions with arrays of element that should pass. I spent too much time as a s/w tester, you see, and am pre-conditioned to test till I break something :wink:

Good luck with things.

The number_format function is also worth looking into.

cough money_format cough