Bitcoin Forum

Bitcoin => Project Development => Topic started by: The_Trader on May 20, 2021, 09:11:39 AM



Title: Multiplying and API output by another API output
Post by: The_Trader on May 20, 2021, 09:11:39 AM
Hi all

So I have built myself a php page to track my BTC holdings, and I am trying to add a value in $ box on the page.

I have an API output that is the current price of BTC (A) and I have an output of my BTC holdings (B) they are both via API.

Is it possible to multiply A by B to get (C - my holding value in $)

Thanks in advance for any help with this, I am on a rented OVH box running command line ubuntu 20xx.

TT



Title: Re: Multiplying and API output by another API output
Post by: TryNinja on May 20, 2021, 09:20:18 AM
Yes, it’s as simple as just doing it. :D

Code:
$holdings = …
$value = …

$result = $holdings * $value


Title: Re: Multiplying and API output by another API output
Post by: The_Trader on May 20, 2021, 09:47:38 AM
Yes, is as simple as just doing it. :D

Code:
$holdings = …
$value = …

$result = $holdings * $value

Thanks that worked, I just coudn't see the wood from the tree's :)

TT


Title: Re: Multiplying and API output by another API output
Post by: The_Trader on May 20, 2021, 04:15:11 PM
Could I ask another question.

So my output reads "$2736.82192175615" I would like to make it read $2736.82
I have searched for an answer but am having issues, I think it is something to do with "sprintf" but I cant get it to work :(

I am using this to fetch and display the price:

Code:
 
$url = "http://chainz.cryptoid.info/btc/api.dws?q=ticker.usd";
$price = json_decode(file_get_contents($url), true);
echo "$price";

Thanks
TT


Title: Re: Multiplying and API output by another API output
Post by: coinableS on May 21, 2021, 03:53:01 AM
Use number_format()

Code:
$value = 1234.7890;
$formattedValue = number_format($value,2);



Title: Re: Multiplying and API output by another API output
Post by: The_Trader on May 21, 2021, 02:28:06 PM
Use number_format()

Code:
$value = 1234.7890;
$formattedValue = number_format($value,2);



Thanks for your reply, how/where do I insert this, because this isn't working the way I have done it. I guess it needs to be placed somewhere else:


$result = $holding * $price;
$value = 1234.7890;
$formattedValue = number_format($value,2);
echo "$result";

Thank you for taking the time to help with this.

TT


Title: Re: Multiplying and API output by another API output
Post by: TryNinja on May 21, 2021, 02:38:29 PM
Thanks for your reply, how/where do I insert this, because this isn't working the way I have done it.
That's basic PHP, just learn how to insert a variable somewhere.

echo $result has no quotes, otherwise you are literally printing the text "$result" and not what's inside the variable.

For example, you can insert that in the middle of your HTML page:
Code:
<div>
  Total value: <#php echo $result; #>
</div>
P.S: change # for ? (the forum is blocking me from using the right code)


Title: Re: Multiplying and API output by another API output
Post by: The_Trader on May 21, 2021, 02:45:38 PM
Thanks for your reply, how/where do I insert this, because this isn't working the way I have done it.
That's basic PHP, just learn how to insert a variable somewhere.

echo $result has no quotes, otherwise you are literally printing the text "$result" and not what's inside the variable.

For example, you can insert that in the middle of your HTML page:
Code:
<div>
  Total value: <#php echo $result; #>
</div>
P.S: change # for ? (the forum is blocking me from using the right code)

Hi I did not realise I could just do that, the issue I am having at present is formating the number out put from for example $1234.567891234566 to $1234.56

Thanks
TT


Title: Re: Multiplying and API output by another API output
Post by: TryNinja on May 21, 2021, 02:53:21 PM
Hi I did not realise I could just do that, the issue I am having at present is formating the number out put from for example $1234.567891234566 to $1234.56
coinableS already told you what you should do:

Use number_format()

Code:
$value = 1234.7890;
$formattedValue = number_format($value,2);

Then, just use the new variable $formattedValue:
Code:
echo $formattedValue;

You should take some time to learn PHP, since this is all very basic. A ~30 minutes Youtube video should be enough to get your started.


Title: Re: Multiplying and API output by another API output
Post by: The_Trader on May 21, 2021, 03:18:14 PM
Hi I did not realise I could just do that, the issue I am having at present is formating the number out put from for example $1234.567891234566 to $1234.56
coinableS already told you what you should do:

Use number_format()

Code:
$value = 1234.7890;
$formattedValue = number_format($value,2);

Then, just use the new variable $formattedValue:
Code:
echo $formattedValue;

You should take some time to learn PHP, since this is all very basic. A ~30 minutes Youtube video should be enough to get your started.

I have been watching videos and reading tutorials, and taking php examples and altering them.
The issue here is I can not figure this out:

Code:
$result = $holding * $price;
$value = 1234.7890;
$formattedValue = number_format($value,2);
echo "$result";

The top and bottom lines are what I am using and they are giving me the correct out put, just the format is too long.
I do not understand where the $value has come from or the numbers 1234.7890.

Should the $value be changed to $result to link up with what i have called it in my bit of code ?

Please understand I am not being lay and have and am learning PHP its just a few things confuse me, and I do appreciate you taking the time to reply.

Thanks
TT

EDIT - figured it out... Thank you for helping


Code:
$result = $holding * $price;
$value = $result;
$formattedValue = number_format($value,2);
echo $formattedValue;