user466534
user466534

Reputation:

question about locale

please explain purpose of usage of locale in c++? i have read documents but dont uderstand please help

Upvotes: 1

Views: 277

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490018

The basic purpose is for localizing applications. For example, in the US a large number with a decimal separator would normally be written like: "1,234.56". Throughout much of Europe the same number would normally be written like: "1.234,56".

A locale allows you to isolate information about such formatting (and other things that vary between countries, languages, cultures, etc.) into one place. For example, I might use:

std::locale loc("");
std::cout.imbue(loc);

std::cout << 1234.56;

The unnamed locale ("") is special: it automatically picks out whatever locale the user has configured. When I run this code, the output I get is: "1,234.56". Somebody else could run exactly the same code, but if their environment was configured for some other convention, they might get "1.234,56" or "1 234,56", etc.

So, most of what the locale buys us (in this case) is keeping writing a number separate from formatting that number appropriately for a specific audience. Of course, a locale has a number of "facets", each of which covers a separate...well, facet of localization, such as formatting numbers, formatting currency, determining what's considered a lower-case or upper-case letter, etc.

Upvotes: 10

Related Questions