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
https://forums.phpfreaks.com/topic/245294-code-breaking-once-applying-or/
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.

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.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.