Bitcoin Forum
May 27, 2024, 04:30:12 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Multiplying and API output by another API output  (Read 106 times)
The_Trader (OP)
Member
**
Offline Offline

Activity: 180
Merit: 52


View Profile
May 20, 2021, 09:11:39 AM
 #1

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

TryNinja
Legendary
*
Offline Offline

Activity: 2842
Merit: 7048


Crypto Swap Exchange


View Profile WWW
May 20, 2021, 09:20:18 AM
Last edit: May 20, 2021, 09:48:27 AM by TryNinja
 #2

Yes, it’s as simple as just doing it. Cheesy

Code:
$holdings = …
$value = …

$result = $holdings * $value

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
The_Trader (OP)
Member
**
Offline Offline

Activity: 180
Merit: 52


View Profile
May 20, 2021, 09:47:38 AM
 #3

Yes, is as simple as just doing it. Cheesy

Code:
$holdings = …
$value = …

$result = $holdings * $value

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

TT
The_Trader (OP)
Member
**
Offline Offline

Activity: 180
Merit: 52


View Profile
May 20, 2021, 04:15:11 PM
 #4

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 Sad

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
coinableS
Legendary
*
Offline Offline

Activity: 1442
Merit: 1179



View Profile WWW
May 21, 2021, 03:53:01 AM
 #5

Use number_format()

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


The_Trader (OP)
Member
**
Offline Offline

Activity: 180
Merit: 52


View Profile
May 21, 2021, 02:28:06 PM
 #6

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
TryNinja
Legendary
*
Offline Offline

Activity: 2842
Merit: 7048


Crypto Swap Exchange


View Profile WWW
May 21, 2021, 02:38:29 PM
 #7

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)

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
The_Trader (OP)
Member
**
Offline Offline

Activity: 180
Merit: 52


View Profile
May 21, 2021, 02:45:38 PM
 #8

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
TryNinja
Legendary
*
Offline Offline

Activity: 2842
Merit: 7048


Crypto Swap Exchange


View Profile WWW
May 21, 2021, 02:53:21 PM
 #9

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.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
The_Trader (OP)
Member
**
Offline Offline

Activity: 180
Merit: 52


View Profile
May 21, 2021, 03:18:14 PM
Merited by TryNinja (1)
 #10

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;


Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!