Using WordPress ‘number_format_i18n()’ PHP function

The number_format_i18n() WordPress PHP function converts a float number to a format based on the locale.

Usage

number_format_i18n( $number, $decimals )

Example:

$number = 3948;
$formatted = number_format_i18n( $number );
// $formatted will now be something like "3,948"

Parameters

  • $number (float) – The number to convert based on the locale.
  • $decimals (int) – Optional. Precision of the number of decimal places. Default is 0.

More information

See WordPress Developer Resources: number_format_i18n()

Examples

Basic Usage

Convert a simple number to a locale-specific format:

$number = 12345.67;
$formatted = number_format_i18n( $number );
// Output: "12,345.67" or "12.345,67" depending on the locale

Formatting with Decimal Places

Specify the number of decimal places:

$number = 12345.6789;
$formatted = number_format_i18n( $number, 2 );
// Output: "12,345.68" or "12.345,68" depending on the locale

Zero Decimal Places

Formatting a number without any decimal places:

$number = 12345.6789;
$formatted = number_format_i18n( $number, 0 );
// Output: "12,345" or "12.345" depending on the locale

Locale-specific Formatting for Currency

Display a currency amount using the locale-specific format:

$amount = 2500.50;
$currency_symbol = '$';
$formatted_amount = $currency_symbol . number_format_i18n( $amount, 2 );
// Output: "$2,500.50" or "$2.500,50" depending on the locale

Displaying Large Numbers

Format a large number with thousands separators:

$number = 9876543210.123456;
$formatted = number_format_i18n( $number, 3 );
// Output: "9,876,543,210.123" or "9.876.543.210,123" depending on the locale