SO I am going to integrate the dogecoin in to the my busniess website. My Products are avilable to purchase in EURO currency and for avilable to provide it in doecoin I need to convert the EURO to Dogecoin.
What I have DID: I have found the DOGECOIN API (https://www.dogeapi.com) in php. i have found that we can convert the DOGECOIN in BTC or USD. Using this :
https://www.dogeapi.com/wow/?a=get_current_price&convert_to=USD&amount_doge=1000
The above URL gives me the total USD from DOGE AMOUNT.output is : 1.13949000
My Question is : How Can i convert the EURO in DOGEAMOUNT ? I have search alot and not found any solution . please help. Thanks in Advance.
A bit messy but does the work (it uses another url to get exchanges for USD,EUR)
$doge2usd = file_get_contents("https://www.dogeapi.com/wow/?a=get_current_price&convert_to=USD&amount_doge=1");
echo sprintf("1 dogecoin => %f usd",$doge2usd); // 1 DOGE => USD 0.00115078
$eur2usd = file_get_contents("http://rate-exchange.appspot.com/currency?from=EUR&to=USD");
$j = json_decode($eur2usd,TRUE);
$doge2eur = $doge2usd * (1 / $j["rate"]); // 1 DOGE => 0.00083941557920536 EUR
echo sprintf("<br>1 dogecoins => %f euro, 5 dogecoins => %f euro",$doge2eur,$doge2eur*5);
$eur2doge = 1 / $doge2eur; // 1 EUR => DOGE 1197.29
echo sprintf("<br>1 euro => %f dogecoins, 5 euro => %f dogecoins",$eur2doge,$eur2doge*5);
The dogeapi.com API can only give you the exchange rate in BTC or USD. To get the exchange rate from XDG (is that the inofficial three-letter-code for Dogecoin? Let's just assume that) to EUR, you'll have to take two steps:
Get the exchange rate from XDG to USD.
Get the exchange rate from USD to EUR.
For the first one, we have DogeAPI. For the second one, I'm going to use Yahoo's API.
<?php
// how much is 1 dogecoin worth in USD?
$xdgusd = (double)file_get_contents("https://www.dogeapi.com/wow/?a=get_current_price&convert_to=USD&amount_doge=1");
// how much is 1 EUR worth in USD?
$yahoo_result = json_decode(file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20=%20%22EURUSD%22&format=json&env=store://datatables.org/alltableswithkeys&callback="));
$eurusd = (double)$yahoo_result->query->results->rate->Rate;
// how much is 1 dogecoin worth in EUR?
$xdgeur = $xdgusd / $eurusd;
echo "Doge in USD: " . $xdgusd . "\n";
echo "EUR in USD: " . $eurusd . "\n";
echo "Doge in EUR: " . $xdgeur . "\n";
This prints:
Doge in USD: 0.00113941
EUR in USD: 1.3713
Doge in EUR: 0.00083089768832495
Please note that this example doesn't cover details like bis/ask spread. Also, in a real system, you shouldn't query the web services with every request, but cache the result on your machine. And check if you got sane values back from the APIs.
Related
Let's say I have 2 numbers like so
1.50
2.00
I want to format the number so that
1.50 will show as 1.5
2.00 will show as 2
Basically if it is not a whole number then show that with ending 0's removed and if it is a whole number to show whole. I was trying number_format('2.00', 2); but that of course keeps the decimals. I was hoping there was a easy way to do this.
Multiply the number with 1 and it will remove any trailing zeros.
$arr = ["1.50","2.00"];
foreach($arr as $v){
echo $v*1 . PHP_EOL;
}
//1.5
//2
Try casting both strings to floats:
echo (float)'1.50';
// => 1.5
echo (float)'2.00';
// => 2
Try it online!
What is the correct way to display rupee as paise with php?
eg,
552.25 rupee = 55225 paise
100.00 rupee = 1000 paise
1 Rupees is equal to 100 Paise (paise is an indian & pakistan currency)
So, you should used this formula
$paise=your_currency*100;
if you have simple 552.25 rupees
$rs=552.25;
$paise=$rs*100;
if you have 552.25 rupee. then
$rs=`552.25 rupee`;
preg_match('/([0-9]+\.[0-9]+)/', $rs, $matches);
$float_val= (float) $matches[1];
$paise=$float_val*100;
Simple, You can do it by this way.
Like below :
$rs = 100*100. " paise";
Output :
10000 paise
I have a amount without 20% Tax in my country.. For example, 12,24 € it s without tax and 14,70 EUR it's with a TAX.
How can I add 20% to 12.24 EUR Amount in PHP?
My script is:
<? echo round($cena*(20/100)+$cena, 2);?>
But with this script is amount with TAX 14,40 EUR and in real it is 14,70 EUR.
Where is problem? And numbers with zero at start it don't calulcate.. It will show 0,- EUR.
Thanks.
Use this
$num = 12.24;
$percentage = 20;
$num += $num*($percentage/100);
$num = round($num, 1); // 4
$num = sprintf('%0.2f', $num);
echo $num;
As mentioned in comments, just multiply that number by 1.2. So:
<?php echo round($cena * 1.2, 2); ?>
if you use calculator to solve it, 14.70 is correct. to get 14.40
<?php echo round($cena * 0.18, 2); ?>
0.18 is 18%
But with this script is amount with TAX 14,40 EUR and in real it is 14,70 EUR. Where is problem?
The problem is with the format of your input, 12,24. It is treated as 12. This is probably because PHP expects the decimal point to be . and not ,.
I'm trying to use money_format in PHP to get this result: $100,000.00 USD if that is possible.
Here is what I have:
$cost = 100000;
$usd = money_format('%i', $cost);
This is what I get:
USD 100,000.00
What I would like is:
$100,000.00 USD
I may be adding other currencies later, so if the solution breaks the general utility and l10n of money_format then I won't use it and will just use the default value.
i prefer number_format()
$usd = '$'.number_format($number, 2, '.', ',').' USD';
we have to make a web game for a school project.
but im currently stuck at one point. its a mafia styled web game, you probably know one of these. When your charachter goes to the hospital to get his wounds fixed up he needs to pay a certain amount of money. this is calculated by the following code
$maxHeal = 100 - $health;
$costs =round(pow($maxHeal,1.8));
health is a number between 0 and 100 and the costs are based on exponential growth. but if the player can only afford 50 but types in 100, how do I make sure he only getw 50, and how do I make sure it are the first 50 health points, the most expensive ones and not the cheap ones, this will cause player to just type in 1 press enter to get some cheap health.
I hope my porblem is clear, if you have any questions about other parts of the codes please ask
thanks in advance
edit: to give some extra clearance,
when I am at 10 health(hp) and I want to go back to 100hp I need to get a 90 extra. there is a form where I can type how much hp I want to cure, so I type in 90 and the system requests to ad 90 to my life so it makes 100. to do this I need to check if the players can afford to pay for those 90 points. if I can not pay for 90 but can pay for 50 I want those 50 to be added anyways. but if I count from 1 to 50 and to one form 40(the remaining I need to heal another timer) it wil cost less than counting from 1 to 90 because of the exponential growth.
so I need 2 checks. I have to cure al I can afford, so if I can afford just 50 of the 90 hp I need, I will only get 50 points and pay for 50, but as this wil be cheaper how can I make sure that I pay for the 50 like I woulld pay for 90. so 50 and 40 need to be equal to one times 90
Based on your question (which is not totally clear to me, but hey, I'm in a good mood), I built the following example:
//total amount of health points
$points = 20000;
//health left
$health = 10;
//how many health do we miss? 100 = maximal
$maxHeal = 100 - $health;
//counter
$i = 0;
while($points > $cost = round(pow($maxHeal-$i,1.8))) {
//check if the user has enough points
if ($points - $cost > 0) {
$health++;
echo "Healt +1 point, total: " . $health . " (points " . $points . " - cost " . $cost . " = " . ($points - $cost) . " left)" . "\n";
} else {
echo "Can't heal anymore, not enough points left (" . $points . ")" . "\n";
}
$points -= $cost;
$i++;
}
echo "\n";
echo "Remaining points: " . $points . ", needs points for next repair: " . $cost;
With the following output:
Health now: 10
Healt +1 point, total: 11 (points 20000 - cost 3293 = 16707 left)
Healt +1 point, total: 12 (points 16707 - cost 3228 = 13479 left)
Healt +1 point, total: 13 (points 13479 - cost 3163 = 10316 left)
Healt +1 point, total: 14 (points 10316 - cost 3098 = 7218 left)
Healt +1 point, total: 15 (points 7218 - cost 3035 = 4183 left)
Healt +1 point, total: 16 (points 4183 - cost 2971 = 1212 left)
Remaining points: 1212, needs points for next repair: 2909
I hope this gets you going :)