number_format_i18n()
Converts a number (integer or decimal) into a format suitable for the current locale (site language).
The number to be formatted can be a price or a quantity of something - a number that should be easily perceivable by a human.
For example, for Russian, thousands are separated by a space, and decimals by a comma - 1 111,11. For English, thousands are separated by a comma and decimals by a dot - 1,111.11.
Works based on the PHP function number_format().
Also see the custom function number_to_human() - Rounding large numbers.
Used By: get_comments_number_text(), size_format()
1 time — 0.000034 sec (very fast) | 50000 times — 0.06 sec (speed of light) | PHP 7.1.2, WP 4.7.4
Hooks from the function
Returns
String. Formatted number.
Usage
number_format_i18n( $number, $decimals = 0 );
- $number(float) (required)
- The number to be formatted according to the locale.
- $decimals(int)
- How many decimal places to leave or show?
Default: 0
Examples
#1 Output demonstration
$number = number_format_i18n( 1111 ); // Russian: '1 111' // English: '1,111' $number = number_format_i18n( 2222, 2 ); // Russian: '2 222,00 // English: '2,222.00' $number = number_format_i18n( 5555.5555555, 2 ); // Russian: '5 555,56 // English: '5,555.56' // Handling empty values: number_format_i18n( 0 ); // "0" number_format_i18n( null ); // "0" number_format_i18n( '' ); // Warning: number_format() expects parameter 1 to be float, string given number_format_i18n( [] ); // Warning: number_format() expects parameter 1 to be float, array given
Notes
- Global. WP_Locale.
$wp_localeWordPress date and time locale object.
Changelog
| Since 2.3.0 | Introduced. |
number_format_i18n() number format i18n code WP 7.0
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 );
}