number_format_i18n() WordPress Function

The number_format_i18n() function is used to format a number for display in a language-specific way. This function is similar to the PHP function number_format(), but it also takes into account the locale setting of the WordPress site. This function is useful for displaying numbers in a way that is consistent with how they are typically displayed in the site's language. For example, if the site is in French, this function will format numbers using the French rules for decimal and thousands separators. This function can be used anywhere that a number needs to be displayed to the user. Some examples include displaying prices, quantities, or percentages.

number_format_i18n( float $number, int $decimals ) #

Convert float number to format based on the locale.


Parameters

$number

(float)(Required)The number to convert based on locale.

$decimals

(int)(Optional) Precision of the number of decimal places. Default 0.


Top ↑

Return

(string) Converted number in string format.


Top ↑

More Information

i18n is an abbreviation for internationalization.


Top ↑

Source

File: wp-includes/functions.php

function number_format_i18n( $number, $decimals = 0 ) {
	global $wp_locale;

	if ( isset( $wp_locale ) ) {
		$formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
	} else {
		$formatted = number_format( $number, absint( $decimals ) );
	}

	/**
	 * Filters the number formatted based on the locale.
	 *
	 * @since 2.8.0
	 * @since 4.9.0 The `$number` and `$decimals` parameters were added.
	 *
	 * @param string $formatted Converted number in string format.
	 * @param float  $number    The number to convert based on locale.
	 * @param int    $decimals  Precision of the number of decimal places.
	 */
	return apply_filters( 'number_format_i18n', $formatted, $number, $decimals );
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More