currency php code example
Example 1: format php currency
<?php
$number = 1234.56;
$english_format_number = number_format($number);
$nombre_format_francais = number_format($number, 2, ',', ' ');
$number = 1234.5678;
$english_format_number = number_format($number, 2, '.', '');
Example 2: php money_format currency symbol
<?php
$number = 1234.56;
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
$number = -1234.5672;
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
echo money_format('%=*(#10.2n', $number) . "\n";
setlocale(LC_MONETARY, 'de_DE');
echo money_format('%=*^-14#8.2i', 1234.56) . "\n";
setlocale(LC_MONETARY, 'en_GB');
$fmt = 'The final value is %i (after a 10%% discount)';
echo money_format($fmt, 1234.56) . "\n";
?>
Example 3: currency convertor in php
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.exchangeratesapi.io/latest?base=USD",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "{\n\t\"QuantityOnHand\":1,\n\t\"ReOrderPoint\":1,\n\t\"QuantityAsOfDate\":\"2018-03-01\"\n}",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"postman-token: f8326c98-2bb7-a466-6f5e-cb8bfc5421d0"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$response;
$response= json_decode($response,true);
$CAD= $response['rates']['CAD'];
$AUD= $response['rates']['AUD'];
$GBP= $response['rates']['GBP'];
?>