Nick11380 Posted August 20, 2011 Share Posted August 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245294-code-breaking-once-applying-or/ Share on other sites More sharing options...
Pikachu2000 Posted August 20, 2011 Share Posted August 20, 2011 Using phone browser, so sorry for being brief, but try enclosing the calculation in parentheses. $newvalue = ( $oldvalue * 0.321 ); [code=php:0] Quote Link to comment https://forums.phpfreaks.com/topic/245294-code-breaking-once-applying-or/#findComment-1259860 Share on other sites More sharing options...
Nick11380 Posted August 20, 2011 Author Share Posted August 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245294-code-breaking-once-applying-or/#findComment-1259864 Share on other sites More sharing options...
PFMaBiSmAd Posted August 20, 2011 Share Posted August 20, 2011 I would use var_dump() on the values to see what they actually contain. Quote Link to comment https://forums.phpfreaks.com/topic/245294-code-breaking-once-applying-or/#findComment-1259866 Share on other sites More sharing options...
Nick11380 Posted August 20, 2011 Author Share Posted August 20, 2011 I would use var_dump() on the values to see what they actually contain. How would I go about that? <?php var_dump($ounce_price); ?> ? (sorry, I'm very new to PHP) Quote Link to comment https://forums.phpfreaks.com/topic/245294-code-breaking-once-applying-or/#findComment-1259869 Share on other sites More sharing options...
Nick11380 Posted August 20, 2011 Author Share Posted August 20, 2011 All fixed, a dude from devnet gave the answer. The script returned a value of '1' because it couldn't deal with numbers over 999. I needed to add this to the $ounce_price = $ounce_price = preg_replace('/[^-0-9.]/','',$nodes[0][1]); Quote Link to comment https://forums.phpfreaks.com/topic/245294-code-breaking-once-applying-or/#findComment-1259886 Share on other sites More sharing options...
PFMaBiSmAd Posted August 20, 2011 Share Posted August 20, 2011 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.) Quote Link to comment https://forums.phpfreaks.com/topic/245294-code-breaking-once-applying-or/#findComment-1259889 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.