Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 \code
38 import QtQuick 2.0
39
40 Text {
41 text: "The value is: " + Number(4742378.423).toLocaleString(Qt.locale("de_DE"))
42 }
43 \endcode
44
45 You can apply toLocaleString() directly to constants, provided the decimal
46 is included in the constant, e.g.
47 \code
48 123.0.toLocaleString(Qt.locale("de_DE")) // OK
49 123..toLocaleString(Qt.locale("de_DE")) // OK
50 123.toLocaleString(Qt.locale("de_DE")) // fails
51 \endcode
52*/
53
54/*!
55 \qmlmethod string Number::toLocaleCurrencyString(locale,symbol)
56
57 Converts the Number to a currency using the currency and conventions of the specified
58 \a locale. If \a symbol is specified it will be used as the currency
59 symbol.
60
61 \sa Locale::currencySymbol()
62*/
63
64/*!
65 \qmlmethod string Number::fromLocaleString(locale,number)
66
67 Returns a Number by parsing \a number using the conventions of the supplied \a locale.
68
69 If \a locale is not supplied the default locale will be used.
70
71 For example, using the German locale:
72 \code
73 var german = Qt.locale("de_DE");
74 var d;
75 d = Number.fromLocaleString(german, "1234,56") // d == 1234.56
76 d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
77 d = Number.fromLocaleString(german, "1234.56") // throws exception
78 d = Number.fromLocaleString(german, "1.234") // d == 1234.0
79 \endcode
80*/