0

I have written a little bit of code to return the value "lastPrice" from the json provided by http://csc.blockexp.info/ext/summary, but it is returning the value as 8.503E-5 instead of the desired lastPrice.

{"data":[{"difficulty":159.63461978,"supply":33494475.4445624,"hashrate":"11.2889","lastPrice":0.00008503,"connections":24,"blockcount":1136720}]}

$jsonCSC = file_get_contents('http://csc.blockexp.info/ext/summary');
$dataCSC = json_decode($jsonCSC,true);
print_r($dataCSC);
$priceCSC = $dataCSC["data"][0]["lastPrice"];

the array prints like so:

Array ( [data] => Array ( [0] => Array ( [difficulty] => 110.1356266 [supply] => 33494245.423562 [hashrate] => 10.5306 [lastPrice] => 8.503E-5 [connections] => 24 [blockcount] => 1136696 ) ) )

As you can see lastPrice is not the last price from the file_get_contents link. I have no idea where the E-5 is coming from and why the value is being returned as 8.503 instead of the floating value any insight would be appreciated.

@enkrates pointed out that is is completely valid as a float with E-5!

Spade
  • 591
  • 3
  • 20
  • The E can be short for `10^`. So 8.503*10^-5 would be 0.00008503 which probably isn't right to begin with. – iam-decoder Nov 12 '15 at 02:51
  • 1
    See [this codepad](http://codepad.viper-7.com/HNDLxV) for an example of it being used as a (valid) number. – Jared Farrish Nov 12 '15 at 02:58
  • @JaredFarrish After some inspection anything after 7 decimal places is turned into the format #E-# but I need to have the 8 decimal places since it is a value. So if I do `$priceCSC / pow(10, -5) / 10000` it is returning it as 0.0008501 but I need it to be 0.00008501. Any thoughts? @iam-decoder that is actually the right answer! – Spade Nov 12 '15 at 03:25
  • 1
    http://codepad.viper-7.com/kaBzHO – Jared Farrish Nov 12 '15 at 03:34
  • @JaredFarrish Exactly what I needed thankyou very much! – Spade Nov 12 '15 at 03:36

1 Answers1

2

8.503E-5 is actually valid PHP syntax for a floating point number. See the PHP documentation for floating point numbers for more information and an example.

enkrates
  • 626
  • 1
  • 6
  • 17
  • Is there any way besides doing math after I retrieve the value to return the value as is from the file_get_contents link? – Spade Nov 12 '15 at 02:58
  • It seems like converting a number in that format can be complicated. See [this SO thread](http://stackoverflow.com/questions/4461444/convert-exponential-number-to-decimal-in-php) for some ideas (warning: may involve PHP extensions). – enkrates Nov 12 '15 at 12:25