Author

Topic: Multiplying and API output by another API output (Read 106 times)

member
Activity: 180
Merit: 52
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;


legendary
Activity: 2758
Merit: 6830
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.
member
Activity: 180
Merit: 52
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:

  Total value: <#php echo $result; #>
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
legendary
Activity: 2758
Merit: 6830
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:

  Total value: <#php echo $result; #>
P.S: change # for ? (the forum is blocking me from using the right code)
member
Activity: 180
Merit: 52
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
legendary
Activity: 1442
Merit: 1179
Use number_format()

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

member
Activity: 180
Merit: 52
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
member
Activity: 180
Merit: 52
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
legendary
Activity: 2758
Merit: 6830
Yes, it’s as simple as just doing it. Cheesy

Code:
$holdings = …
$value = …

$result = $holdings * $value
member
Activity: 180
Merit: 52
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

Jump to: