SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    Hikari silver trophy artistsneverdie's Avatar
    Join Date
    Oct 2004
    Location
    essex, uk
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:


    PHP Code:
    $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
    www.hikaridesign.com | Contemporary Design
    Won some awards here once upon a time...

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  3. #3
    Hikari silver trophy artistsneverdie's Avatar
    Join Date
    Oct 2004
    Location
    essex, uk
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry my bad, it doesn't return the denomination,

    So basically:

    Code:
    <currentPrice currencyId="GBP">200.0</currentPrice>
    <convertedCurrentPrice currencyId="GBP">200.0</convertedCurrentPrice>
    Is what is received back from the XML request.
    www.hikaridesign.com | Contemporary Design
    Won some awards here once upon a time...

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In light of edit2 above, run this lot through sprintf for a few surprises:

    PHP Code:
    $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?

  5. #5
    Hikari silver trophy artistsneverdie's Avatar
    Join Date
    Oct 2004
    Location
    essex, uk
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    www.hikaridesign.com | Contemporary Design
    Won some awards here once upon a time...

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So, reading up on sprintf, and using the example I suggested from the man:
    PHP Code:
    $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

  7. #7
    Hikari silver trophy artistsneverdie's Avatar
    Join Date
    Oct 2004
    Location
    essex, uk
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    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.
    www.hikaridesign.com | Contemporary Design
    Won some awards here once upon a time...

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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

    Good luck with things.

  9. #9
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The number_format function is also worth looking into.

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    *cough* money_format *cough*
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •