WordPress как на ладони
rgbcode is looking for WordPress developers. Очень Удобный и Быстрый Хостинг для сайтов на WordPress. Пользуюсь сам и вам рекомендую!

number_format_i18n()WP 2.3.0

Конвертирует число (целое или дробное) в формат подходящий к текущей локали (языку сайта).

Числом, которое нужно отформатировать может быть цена или количество чего-либо - число которое должно легко восприниматься человеком.

Например, для русского тысячи отделяются пробелом, а дробные запятой - 1 111,11. А для англ. тысячи запятой а дробные точкой - 1,111.11.

Работает на базе PHP функции number_format().

Смотрите также кастомную функцию number_to_human() - Округление больших чисел.

1 раз — 0.000034 сек (очень быстро) | 50000 раз — 0.06 сек (скорость света) | PHP 7.1.2, WP 4.7.4
Хуки из функции

Возвращает

Строку. Отформатированное число.

Использование

number_format_i18n( $number, $decimals = 0 );
$number(float) (обязательный)
Число которое нужно отформатировать в соответствии с локалью.
$decimals(число)
Сколько знаков после запятой оставить или показать?
По умолчанию: 0

Примеры

0

#1 Демонстрация вывода

$number = number_format_i18n( 1111 );
// рус:  '1 111'
// англ: '1,111'

$number = number_format_i18n( 2222, 2 );
// рус:  '2 222,00'
// англ: '2,222.00'

$number = number_format_i18n( 5555.5555555, 2 );
// рус:  '5 555,56'
// англ: '5,555.56'

// Обработка пустых значений:

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

Заметки

  • Global. WP_Locale. $wp_locale WordPress date and time locale object.

Список изменений

С версии 2.3.0 Введена.

Код number_format_i18n() WP 6.4.3

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 );
}
2 комментария
    Войти