1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
7 \brief The Number object provides represents a number value.
9 The QML Number object extends the JS Number object with
10 locale aware functions.
12 \sa {QtQml::Locale}{Locale}
16 \qmlmethod string Number::toLocaleString(locale,format,precision)
18 Converts the Number to a string suitable for the specified \a locale
19 in the specified \a format, with the specified \a precision.
23 \li 'f' Decimal floating point, e.g. 248.65
24 \li 'e' Scientific notation using e character, e.g. 2.4865e+2
25 \li 'E' Scientific notation using E character, e.g. 2.4865E+2
26 \li 'g' Use the shorter of e or f
27 \li 'G' Use the shorter of E or f
30 If precision is not specified, the precision will be 2.
32 If the format is not specified 'f' will be used.
34 If \a locale is not specified, the default locale will be used.
36 The following example shows a number formatted for the German locale:
41 text: "The value is: " + Number(4742378.423).toLocaleString(Qt.locale("de_DE"))
45 You can customize individual fields of the \a{locale} to tightly control
48 let locale = Qt.locale("de_DE");
49 let a = Number(1000).toLocaleString(locale)); // a == 1.000,00
50 locale.numberOptions = Locale.OmitGroupSeparator;
51 let b = Number(1000).toLocaleString(locale)); // b == 1000,00
54 You can apply toLocaleString() directly to constants, provided the decimal
55 is included in the constant, e.g.
57 123.0.toLocaleString(Qt.locale("de_DE")) // OK
58 123..toLocaleString(Qt.locale("de_DE")) // OK
59 123.toLocaleString(Qt.locale("de_DE")) // fails
62 \sa {QtQml::Locale}{Locale}
66 \qmlmethod string Number::toLocaleCurrencyString(locale,symbol)
68 Converts the Number to a currency using the currency and conventions of the specified
69 \a locale. If \a symbol is specified it will be used as the currency
72 \sa Locale::currencySymbol()
76 \qmlmethod string Number::fromLocaleString(locale,number)
78 Returns a Number by parsing \a number using the conventions of the supplied \a locale.
80 If \a locale is not supplied the default locale will be used.
82 For example, using the German locale:
84 var german = Qt.locale("de_DE");
86 d = Number.fromLocaleString(german, "1234,56") // d == 1234.56
87 d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
88 d = Number.fromLocaleString(german, "1234.56") // throws exception
89 d = Number.fromLocaleString(german, "1.234") // d == 1234.0
92 \sa {QtQml::Locale}{Locale}