Jump to content

Code breaking once applying '+' '/' '*' or '-'


Nick11380

Recommended Posts

I have gold data being reported here goldprices.org.uk (scroll down near the bottom). Recently it broke for no apparent reason. I checked the scraper and everything seems to be OK. The issue is that the gold price in (troy) ounces is being scraped fine - however to work out the price in grams you must multiply by 0.0321 (grams in a troy ounce). The code looks like this:

 

$ounce_price = null;
    $grams_price = null;
    if(count($nodes) == 1 && $nodes[0][1]) {
        $ounce_price = $nodes[0][1];
        $grams_price = $ounce_price * 0.0321;

 

However $ounce_price * 0.0321 breaks the code and returns '0.0321'.

 

I then tried the code:

 

$ounce_price = null;
    $grams_price = null;
    if(count($nodes) == 1 && $nodes[0][1]) {
        $ounce_price = $nodes[0][1];
        $grams_price = $ounce_price + 1;

 

And the code returned the value '2'. So it appears that when $ounce_price is being multiplied/subtracted etc it reverts to a value of '1'. However if I do $grams_price = $ounce_price the value is the correct ounce price.

 

I'm so confused as to why when adding an equation to $ounce_price the value reverts to '1' as opposed to equalling the correct number. Any help here would be HUGELY appreciated - I've been stuck for several days and only just decided to ask online :s

 

Nick

Link to comment
Share on other sites

Thanks for the reply Pikachu. Unfortunately that hasn't worked.

 

It's so odd how $ounce_price returns a value but if you try to multiply it, it reverts to '1'. I am willing to pay a small amount for anyone who could run through the entire code and spot the problem. I'm racking my brain thinking about this one.

Link to comment
Share on other sites

It sounds like the actual problem was that there was a comma as a thousands separator in the number (the thousands separator is a human convention to make numbers easier for us to read.) Computer programs don't need and some cannot make use of things like commas in numbers (php treats the first character that is not valid for a number as a stop character.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.