Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Price of Precious Metals

    Hey guys. New to this forum, but need some help. I've managed to get an XML file that has current prices, but there are some problems that I can't figure out. The 3 prices that do show up are all incorrect, and the price of silver shows 0 because the price per gram is under $1 (I tested it).
    Here's the code:
    PHP Code:
    $completeurl "http://www.xmlcharts.com/cache/precious-metals.php";
    $xml simplexml_load_file($completeurl);
    $gold ceil($xml->currency[11]->price[0]*3110.34768)/100;
    $silver ceil($xml->currency[11]->price[3]*3110.34768)/100;
    $platinum ceil($xml->currency[11]->price[2]*3110.34768)/100;
    $palladium ceil($xml->currency[11]->price[1]*3110.34768)/100;

    echo 
    "Current price of precious metals (USD/ozt)<br/>";
    echo 
    "Gold: $".$gold."<br/>";
    echo 
    "Silver: $".$silver."<br/>";
    echo 
    "Platinum: $".$platinum."<br/>";
    echo 
    "Palladium: $".$palladium."<br/>"

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    These aren't numbers, they are strings. Which may lead to problems. Cast them first.
    PHP Code:
    $gold ceil((float)$xml->currency[11]->price[0]*3110.34768)/100;
    $silver ceil((float)$xml->currency[11]->price[3]*3110.34768)/100;
    $platinum ceil((float)$xml->currency[11]->price[2]*3110.34768)/100;
    $palladium ceil((float)$xml->currency[11]->price[1]*3110.34768)/100
    I haven't a clue what the 3110.34768 is for, but I assume that you do :P
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's the number of grams in a troy ounce times 100 (times 100 is for the rounding)

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Of course! That makes sense now.
    Although the mathematics still has me questioning. With the 100x multiplier and the division, are you intending to get an round number? If so, you can just either truncate it completely by cast the results to an int, or using a printf or round function to actually move the a whole number. Then you simply divide by the 31.1034768 instead.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did that to make it round up to the hundredth, since I don't think PHP has a function that does that.

    I just tested your solution and it works. Thanks a ton!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sure it does, you can round using either the printf or the round() functionality:
    PHP Code:
    $ppg 1577.1997670959;
    printf("%0.2f"$ppg);
    // results in 1577.20 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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