|  | Home · Overviews · Examples |

# QLocale Class Reference
[com.trolltech.qt.core module]

The `QLocale` class converts between numbers and their string representations in various languages. More...

## Detailed Description

The `QLocale` class converts between numbers and their string representations in various languages.

`QLocale` is initialized with a language/country pair in its constructor and offers number-to-string and string-to-number conversion functions similar to those in `QString`.

Example:

        QLocale egyptian(QLocale::Arabic, QLocale::Egypt);
        QString s1 = egyptian.toString(1.571429E+07, 'e');
        QString s2 = egyptian.toString(10);

        double d = egyptian.toDouble(s1);
        int i = egyptian.toInt(s2);

`QLocale` supports the concept of a default locale, which is determined from the system's locale settings at application startup. The default locale can be changed by calling the static member `setDefault`. Setting the default locale has the following effects:

- If a `QLocale` object is constructed with the default constructor, it will use the default locale's settings.
- QString::toInt(), QString::toDouble(), etc., interpret the string according to the default locale. If this fails, it falls back on the "C" locale.
- QString::arg() uses the default locale to format a number when its position specifier in the format string contains an 'L', e.g. "%L1".

The following example illustrates how to use `QLocale` directly:

        QLocale::setDefault(QLocale(QLocale::Hebrew, QLocale::Israel));
        QLocale hebrew; // Constructs a default QLocale
        QString s1 = hebrew.toString(15714.3, 'e');

        bool ok;
        double d;

        QLocale::setDefault(QLocale::C);
        d = QString("1234,56").toDouble(&ok);   // ok == false
        d = QString("1234.56").toDouble(&ok);   // ok == true, d == 1234.56

        QLocale::setDefault(QLocale::German);
        d = QString("1234,56").toDouble(&ok);   // ok == true, d == 1234.56
        d = QString("1234.56").toDouble(&ok);   // ok == true, d == 1234.56

        QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
        str = QString("%1 %L2 %L3")
              .arg(12345).arg(12345).arg(12345, 0, 16);
        // str == "12345 12,345 3039"

When a language/country pair is specified in the constructor, one of three things can happen:

- If the language/country pair is found in the database, it is used.
- If the language is found but the country is not, or if the country is `AnyCountry`, the language is used with the most appropriate available country (for example, Germany for German),
- If neither the language nor the country are found, `QLocale` defaults to the default locale (see `setDefault`).

The "C" locale is identical to English/`UnitedStates`.

Use `language` and `country` to determine the actual language and country values used.

An alternative method for constructing a `QLocale` object is by specifying the locale name.

        QLocale korean("ko");
        QLocale swiss("de_CH");

This constructor converts the locale name to a language/country pair; it does not use the system locale database.

The double-to-string and string-to-double conversion functions are covered by the following licenses:

Copyright (c) 1991 by AT&T.

Permission to use, copy, modify, and distribute this software for any purpose without fee is hereby granted, provided that this entire notice is included in all copies of any software which is or includes a copy or modification of this software and in all copies of the supporting documentation for such software.

THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.

This product includes software developed by the University of California, Berkeley and its contributors.

See also `QString::arg`, `QString::toInt`, and `QString::toDouble`.