number format for currency in 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: number format to float php
$num = '1,200,998.255';
echo filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
echo filter_var($num, FILTER_SANITIZE_NUMBER_INT);
Example 3: format money with commas in php
<?php
function CurrencyFormat($number)
{
$decimalplaces = 2;
$decimalcharacter = '.';
$thousandseparater = ',';
return number_format($number,$decimalplaces,$decimalcharacter,$thousandseparater);
}
?>
Example 4: 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'];
?>