This tutorial help to format a number using numberformatter. We’ll format a Number to a Dollar Amount in PHP Using the NumberFormatter::formatCurrency Function.
I’ll convert a number in currency format using number_format(the old way) as well as the latest way using NumberFormatter.
This is the latest and arguably the easiest method to format numbers to strings showing different currencies. Please make sure the extension=intl
is enabled in php.ini.
string numfmt_format_currency ( NumberFormatter $fmt , float $Amount , string $currency )
There are three parameters:
NumberFormatter
object.ISO 4217
dictates the currency to use.It returns a String representing the formatted currency value.
Let’s take an example, If the number is 9988776.65 the results will be:
9 988 776,65 € in France 9.988.776,65 € in Germany $9,988,776.65 in the United States
The number format is a very common method to format a number using PHP.
number_format(amount, decimals, decimal_separator, thousands_separator)
.
The sample code to convert a number in currency format:
<?php $amount = 4533.44; $usd = "$" . number_format($amount, 2, ".", ","); echo $usd; ?>
Output:
$4,533.44
This is the easiest way to format a number in PHP 7, but make sure that extension=intl
is enabled in php.ini
.
<?php $amount = 4533.44; $nf = new NumberFormatter("en_US", NumberFormatter::CURRENCY); $usd = $nf->formatCurrency($amount, "USD"); echo $usd;
Output:
$4,533.44
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More