Difference between revisions of "cpp/locale/LC categories"
From cppreference.com
m (see also to c) |
Andreas Krug (Talk | contribs) m (http -> https, geaders sorted) |
||
(8 intermediate revisions by 4 users not shown) | |||
Line 3: | Line 3: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header|clocale}} | {{dcl header|clocale}} | ||
− | {{dcl | | + | {{dcl| |
− | #define LC_ALL /*implementation defined*/ | + | #define LC_ALL /* implementation defined */ |
}} | }} | ||
− | {{dcl | | + | {{dcl| |
− | #define LC_COLLATE /*implementation defined*/ | + | #define LC_COLLATE /* implementation defined */ |
}} | }} | ||
− | {{dcl | | + | {{dcl| |
− | #define LC_CTYPE /*implementation defined*/ | + | #define LC_CTYPE /* implementation defined */ |
}} | }} | ||
− | {{dcl | | + | {{dcl| |
− | #define LC_MONETARY /*implementation defined*/ | + | #define LC_MONETARY /* implementation defined */ |
}} | }} | ||
− | {{dcl | | + | {{dcl| |
− | #define LC_NUMERIC /*implementation defined*/ | + | #define LC_NUMERIC /* implementation defined */ |
}} | }} | ||
− | {{dcl | | + | {{dcl| |
− | #define LC_TIME /*implementation defined*/ | + | #define LC_TIME /* implementation defined */ |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Line 26: | Line 26: | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem | Constant | Explanation}} | + | {{dsc hitem|Constant|Explanation}} |
− | {{dsc | {{tt|LC_ALL}} | + | {{dsc|{{tt|LC_ALL}}|selects the entire C locale}} |
− | {{dsc | {{tt|LC_COLLATE}} | + | {{dsc|{{tt|LC_COLLATE}}|selects the collation category of the C locale}} |
− | {{dsc | {{tt|LC_CTYPE}} | + | {{dsc|{{tt|LC_CTYPE}}|selects the character classification category of the C locale}} |
− | {{dsc | {{tt|LC_MONETARY}} | selects the monetary formatting category of the C locale}} | + | {{dsc|{{tt|LC_MONETARY}}|selects the monetary formatting category of the C locale}} |
− | {{dsc | {{tt|LC_NUMERIC}} | + | {{dsc|{{tt|LC_NUMERIC}}|selects the numeric formatting category of the C locale}} |
− | {{dsc | {{tt|LC_TIME}} | + | {{dsc|{{tt|LC_TIME}}|selects the time formatting category of the C locale}} |
{{dsc end}} | {{dsc end}} | ||
− | Additional macro constants, with names that begin with {{tt|LC_}} followed by at least one uppercase letter, may be defined in {{ | + | Additional macro constants, with names that begin with {{tt|LC_}} followed by at least one uppercase letter, may be defined in {{header|clocale}}. For example, the POSIX specification requires {{tt|LC_MESSAGES}} (which controls {{lc|std::perror}} and {{lc|std::strerror}}), ISO/IEC 30112:2014 ([https://www.open-std.org/JTC1/SC35/WG5/docs/30112d10.pdf 2014 draft]) additionally defines {{tt|LC_IDENTIFICATION}}, {{tt|LC_XLITERATE}}, {{tt|LC_NAME}}, {{tt|LC_ADDRESS}}, {{tt|LC_TELEPHONE}}, {{tt|LC_PAPER}}, {{tt|LC_MEASUREMENT}}, and {{tt|LC_KEYBOARD}}, which are supported by the GNU C library (except for {{tt|LC_XLITERATE}}). |
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
− | + | ||
− | + | ||
#include <clocale> | #include <clocale> | ||
+ | #include <cstdio> | ||
#include <ctime> | #include <ctime> | ||
#include <cwchar> | #include <cwchar> | ||
Line 48: | Line 47: | ||
int main() | int main() | ||
{ | { | ||
− | + | // the C locale will be the UTF-8 enabled English: | |
− | std::setlocale( | + | std::setlocale(LC_ALL, "en_US.UTF-8"); |
− | std::setlocale( | + | |
+ | // decimal dot will be German: | ||
+ | std::setlocale(LC_NUMERIC, "de_DE.UTF-8"); | ||
+ | |||
+ | // date/time formatting will be Japanese: | ||
+ | std::setlocale(LC_TIME, "ja_JP.UTF-8"); | ||
+ | |||
wchar_t str[100]; | wchar_t str[100]; | ||
std::time_t t = std::time(nullptr); | std::time_t t = std::time(nullptr); | ||
Line 56: | Line 61: | ||
std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str); | std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str); | ||
} | } | ||
− | + | |output= | |
Number: 3,14 | Number: 3,14 | ||
− | Date: | + | Date: 日曜日 2022年11月06日 17時55分10秒 |
}} | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/locale/dsc setlocale}} | + | {{dsc inc|cpp/locale/dsc setlocale}} |
− | {{dsc inc | cpp/locale/dsc locale}} | + | {{dsc inc|cpp/locale/dsc locale}} |
− | {{dsc see c | c/locale/LC_categories | locale categories | nomono=true}} | + | {{dsc see c|c/locale/LC_categories|locale categories|nomono=true}} |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 10:12, 13 September 2023
Defined in header <clocale>
|
||
#define LC_ALL /* implementation defined */ |
||
#define LC_COLLATE /* implementation defined */ |
||
#define LC_CTYPE /* implementation defined */ |
||
#define LC_MONETARY /* implementation defined */ |
||
#define LC_NUMERIC /* implementation defined */ |
||
#define LC_TIME /* implementation defined */ |
||
Each of the above macro constants expand to integer constant expressions with distinct values that are suitable for use as the first argument of std::setlocale.
Constant | Explanation |
LC_ALL
|
selects the entire C locale |
LC_COLLATE
|
selects the collation category of the C locale |
LC_CTYPE
|
selects the character classification category of the C locale |
LC_MONETARY
|
selects the monetary formatting category of the C locale |
LC_NUMERIC
|
selects the numeric formatting category of the C locale |
LC_TIME
|
selects the time formatting category of the C locale |
Additional macro constants, with names that begin with LC_
followed by at least one uppercase letter, may be defined in <clocale>. For example, the POSIX specification requires LC_MESSAGES
(which controls std::perror and std::strerror), ISO/IEC 30112:2014 (2014 draft) additionally defines LC_IDENTIFICATION
, LC_XLITERATE
, LC_NAME
, LC_ADDRESS
, LC_TELEPHONE
, LC_PAPER
, LC_MEASUREMENT
, and LC_KEYBOARD
, which are supported by the GNU C library (except for LC_XLITERATE
).
[edit] Example
Run this code
#include <clocale> #include <cstdio> #include <ctime> #include <cwchar> int main() { // the C locale will be the UTF-8 enabled English: std::setlocale(LC_ALL, "en_US.UTF-8"); // decimal dot will be German: std::setlocale(LC_NUMERIC, "de_DE.UTF-8"); // date/time formatting will be Japanese: std::setlocale(LC_TIME, "ja_JP.UTF-8"); wchar_t str[100]; std::time_t t = std::time(nullptr); std::wcsftime(str, 100, L"%A %c", std::localtime(&t)); std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str); }
Output:
Number: 3,14 Date: 日曜日 2022年11月06日 17時55分10秒
[edit] See also
gets and sets the current C locale (function) | |
set of polymorphic facets that encapsulate cultural differences (class) | |
C documentation for locale categories
|