localeconv()
Our article is about the PHP function localeconv(), which is used to get the formatting information for a specified locale. This function is useful for working
The PHP localeconv() function returns an associative array describing how numbers and currency should be formatted in the currently active locale — which decimal point to use, which thousands separator, the currency symbol, where the symbol and sign go, and so on. It reads whatever locale was last set with setlocale(), so it is the bridge between "what locale am I in" and "what do the formatting rules actually say."
This page covers the syntax, every key in the returned array, a runnable example, and the gotcha that trips up most people: the default locale returns mostly empty values.
Syntax
localeconv(): arraylocaleconv() takes no arguments and always returns an array. It has no failure mode — there is no value to pass, and it never returns false.
A basic example
Notice the setlocale() call on the first line. Without it, the script runs in the default C locale, where most monetary fields come back empty — see the gotcha below.
The returned array
localeconv() returns these keys. The plain keys (decimal_point, thousands_sep) apply to ordinary numbers; the mon_* keys apply specifically to monetary values.
| Key | Meaning |
|---|---|
decimal_point | Decimal separator for non-monetary numbers (e.g. .) |
thousands_sep | Thousands separator for non-monetary numbers (e.g. ,) |
grouping | Array describing digit grouping, e.g. [3] = groups of 3 |
int_curr_symbol | International currency symbol, e.g. USD |
currency_symbol | Local currency symbol, e.g. $ |
mon_decimal_point | Decimal separator for money |
mon_thousands_sep | Thousands separator for money |
mon_grouping | Digit grouping for money |
positive_sign / negative_sign | Sign strings for positive / negative values |
int_frac_digits / frac_digits | Number of fractional digits (international / local) |
p_cs_precedes / n_cs_precedes | 1 if the currency symbol precedes a positive / negative value, 0 if it follows |
p_sep_by_space / n_sep_by_space | 1 if a space separates symbol and value |
p_sign_posn / n_sign_posn | Where the sign sits relative to the value and symbol |
To inspect the whole array at once, use print_r():
<?php
setlocale(LC_ALL, 'en_US.UTF-8');
print_r(localeconv());
?>For an en_US.UTF-8 system this prints (abbreviated):
Array
(
[decimal_point] => .
[thousands_sep] => ,
[int_curr_symbol] => USD
[currency_symbol] => $
[frac_digits] => 2
[p_cs_precedes] => 1
[grouping] => Array ( [0] => 3 )
...
)Gotcha: the default locale returns empty values
Before you call localeconv(), PHP runs in the C locale unless you set one. In the C locale almost every monetary field is empty and the digit-count fields are 127 (the C standard's "value not available" marker):
<?php
// No setlocale() — default C locale.
$info = localeconv();
var_dump($info['currency_symbol']); // string(0) ""
var_dump($info['frac_digits']); // int(127)
?>So if localeconv() seems to "return nothing useful," you almost certainly forgot to call setlocale() first. Set the locale explicitly, and be aware that the available locales depend on the operating system the script runs on.
When would I use it?
You rarely call localeconv() to format output by hand — number_format() and the NumberFormatter class from the intl extension do that for you. It is most useful when you need the raw locale rules, for example to:
- build a custom formatter that respects the user's locale,
- normalize user input (knowing whether
1.234,56uses,or.as the decimal point), - decide whether the currency symbol goes before or after the amount.
Related functions
setlocale()— set the locale thatlocaleconv()reads from.number_format()— format a number with grouped thousands.money_format()— locale-aware currency formatting (removed in PHP 8.0).strftime()— locale-aware date/time formatting.