Php

PHP NumberFormatter with Examples

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.

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.

Syntax:

string numfmt_format_currency ( NumberFormatter $fmt , float $Amount , string $currency )

There are three parameters:

  • Formatter: This is the NumberFormatter object.
  • Amount: which is the numeric currency value.
  • Currency: ISO 4217 dictates the currency to use.

It returns a String representing the formatted currency value.

Example:

Let’s take an example, If the number is 9988776.65 the results will be:

Related Post
9 988 776,65 € in France
9.988.776,65 € in Germany
$9,988,776.65 in the United States

Option 1: NUMBER FORMAT

The number format is a very common method to format a number using PHP.

Syntax:

number_format(amount, decimals, decimal_separator, thousands_separator).

  • amount: The number being formatted.
  • decimals: Sets the number of decimal digits. If 0, the decimal_separator is omitted from the return value.
  • decimal_separator: Sets the separator for the decimal point.
  • thousands_separator: Sets the 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

Option 2: NUMBER FORMATTER

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

REFERENCES

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

3 months ago

What is the Purpose of php_eol in PHP?

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

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories