Current Location: Home> Latest Articles> setlocale() Practice and skills for setting number formatting

setlocale() Practice and skills for setting number formatting

gitbox 2025-06-03

In PHP development, it is often necessary to format numbers according to the habits of different regions, especially the expressions of currency amounts, decimal points and thousands. PHP provides the setlocale() function to set region information, thereby affecting the display method of formats such as numbers and dates. This article will explain in detail how to use setlocale() to setnumber formats, as well as related format control techniques and practical examples.


1. What is setlocale() ?

setlocale() is a function used in PHP to set locale settings. It affects the display format of numbers, currencies, dates and times, allowing the program to format output according to specified regional rules.

Function prototype:

 setlocale(int $category, string|array $locale): string|false
  • $category : Set categories, such as LC_NUMERIC (numeric format), LC_MONETARY (currency format), LC_TIME (time format), etc.

  • $locale : Specify the region language code, such as zh_CN.UTF-8 , en_US.UTF-8 , etc.


2. How to set the number format using setlocale()

By default, PHP's numeric format may not comply with the specifications in certain regions, such as the default decimal point in English is . , and the thousandth separator is , . If you want to adjust these formats, you need to use setlocale() .

Example: Set to Chinese environment and adjust the digital format

 <?php
// Set the number format to Chinese environment
setlocale(LC_NUMERIC, 'zh_CN.UTF-8');

// Test digital format
$number = 1234567.89;

// use number_format() Format numbers
echo number_format($number, 2, ',', ' ');
?>

In the above example:

  • Specify the digital format environment through setlocale() .

  • Customize the decimal point and thousands of separator characters with number_format() .

It should be noted that the support of setlocale(LC_NUMERIC, ...) may vary on different systems and PHP versions, and the available language codes on Windows and Linux environments are not exactly the same.


3. Combining localeconv() to obtain localized numeric format parameters

There is also a function localeconv() in PHP, which will return the number format information in the current setlocale() environment, including decimal symbols, thousands of symbols, etc.

Example:

 <?php
// Set the area to German(Germany)
setlocale(LC_ALL, 'de_DE.UTF-8');

// Get the current localized format information
$locale_info = localeconv();

$number = 1234567.89;

echo "Decimal point: " . $locale_info['decimal_point'] . "\n";
echo "Thousands separator: " . $locale_info['thousands_sep'] . "\n";

// use locale 信息Format numbers
echo number_format(
    $number,
    2,
    $locale_info['decimal_point'],
    $locale_info['thousands_sep']
);
?>

Output:

 Decimal point: ,
Thousands separator: .
1.234.567,89

In this example, the German number format used for decimal points , , and used for millites . , in line with German customs.


4. Practical skills

  1. Priority to specifying accurate area codes <br> The locale codes supported by different operating systems are different. You can pass them into an array. PHP will try in turn:

 setlocale(LC_ALL, ['zh_CN.UTF-8', 'zh_CN', 'Chinese']);
  1. Combining localeconv() and number_format() to implement dynamic format <br> Dynamically format numbers based on the separator obtained by the current locale.

  2. When processing currency formats, use LC_MONETARY and combine money_format() (note that PHP7 will not support it later)
    Modern projects recommend using the NumberFormatter class.


5. Example: Dynamic output of formatted numbers according to the region

 <?php
// Setting up the locale
setlocale(LC_ALL, 'fr_FR.UTF-8');

// Get localized digital format information
$locale = localeconv();

$number = 9876543.21;

// 输出Format numbers
echo number_format($number, 2, $locale['decimal_point'], $locale['thousands_sep']);
?>

Running results:

 9 876 543,21

France is used to use spaces as the thousandths, and the decimal point is a comma.


6. Summary

  • setlocale() can control the locale environment in the numeric format.

  • localeconv() provides specific separators for localized numeric format.

  • number_format() combined with localeconv() parameter can flexibly format numbers.

  • Pay attention to the differences in language codes of different systems and adjust them in the test environment.

  • In modern projects, if you need stronger international support, you recommend PHP's NumberFormatter class.

By rationally using setlocale() and related functions, PHP developers can easily realize digital format display that meets the habits of different regions, improving the international experience of the application.


 <?php
// example:set uplocaleIt is Simplified Chinese,Format numbers输出
setlocale(LC_ALL, 'zh_CN.UTF-8');

$number = 1234567.89;
$locale = localeconv();

echo number_format($number, 2, $locale['decimal_point'], $locale['thousands_sep']);
?>