Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
number.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2017 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\qmltype Number
6
\inqmlmodule QtQml
7
\brief The Number object provides represents a number value.
8
9
The QML Number object extends the JS Number object with
10
locale aware functions.
11
12
\sa {QtQml::Locale}{Locale}
13
*/
14
15
/*!
16
\qmlmethod string Number::toLocaleString(locale,format,precision)
17
18
Converts the Number to a string suitable for the specified \a locale
19
in the specified \a format, with the specified \a precision.
20
21
Valid formats are:
22
\list
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
28
\endlist
29
30
If precision is not specified, the precision will be 2.
31
32
If the format is not specified 'f' will be used.
33
34
If \a locale is not specified, the default locale will be used.
35
36
The following example shows a number formatted for the German locale:
37
\qml
38
import QtQuick 2.0
39
40
Text {
41
text: "The value is: " + Number(4742378.423).toLocaleString(Qt.locale("de_DE"))
42
}
43
\endqml
44
45
You can customize individual fields of the \a{locale} to tightly control
46
the output:
47
\qml
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
52
\endqml
53
54
You can apply toLocaleString() directly to constants, provided the decimal
55
is included in the constant, e.g.
56
\qml
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
60
\endqml
61
62
\sa {QtQml::Locale}{Locale}
63
*/
64
65
/*!
66
\qmlmethod string Number::toLocaleCurrencyString(locale,symbol)
67
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
70
symbol.
71
72
\sa Locale::currencySymbol()
73
*/
74
75
/*!
76
\qmlmethod string Number::fromLocaleString(locale,number)
77
78
Returns a Number by parsing \a number using the conventions of the supplied \a locale.
79
80
If \a locale is not supplied the default locale will be used.
81
82
For example, using the German locale:
83
\qml
84
var german = Qt.locale("de_DE");
85
var d;
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
90
\endqml
91
92
\sa {QtQml::Locale}{Locale}
93
*/
qtdeclarative
src
qml
doc
src
javascript
number.qdoc
Generated on
for Qt by
1.14.0