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 \page qml-qtqml-number.html
6 \title The Number JavaScript Object
7
8 \inqmlmodule QtQml
9 \brief The Number object provides represents a number value.
10
11 The QML Number object extends the JS Number object with
12 locale aware functions.
13
14 \sa {QtQml::Locale}{Locale}
15
16 \section1 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 \section1 string Number::toLocaleCurrencyString(locale, symbol)
65
66 Converts the Number to a currency using the currency and conventions of the specified
67 \a locale. If \a symbol is specified it will be used as the currency
68 symbol.
69
70 \sa Locale::currencySymbol()
71
72 \section1 string Number::fromLocaleString(locale, number)
73
74 Returns a Number by parsing \a number using the conventions of the supplied \a locale.
75
76 If \a locale is not supplied the default locale will be used.
77
78 For example, using the German locale:
79 \qml
80 var german = Qt.locale("de_DE");
81 var d;
82 d = Number.fromLocaleString(german, "1234,56") // d == 1234.56
83 d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
84 d = Number.fromLocaleString(german, "1234.56") // throws exception
85 d = Number.fromLocaleString(german, "1.234") // d == 1234.0
86 \endqml
87
88 \sa {QtQml::Locale}{Locale}
89*/