aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/doc/src/javascript/number.qdoc
blob: a37fd8051bf3fbd73723f050edba4006f9bdddd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \qmltype Number
    \inqmlmodule QtQml
    \brief The Number object provides represents a number value.

    The QML Number object extends the JS Number object with
    locale aware functions.

    \sa {QtQml::Locale}{Locale}
*/

/*!
    \qmlmethod string Number::toLocaleString(locale,format,precision)

    Converts the Number to a string suitable for the specified \a locale
    in the specified \a format, with the specified \a precision.

    Valid formats are:
    \list
    \li 'f'   Decimal floating point, e.g. 248.65
    \li 'e'   Scientific notation using e character, e.g. 2.4865e+2
    \li 'E'   Scientific notation using E character, e.g. 2.4865E+2
    \li 'g'   Use the shorter of e or f
    \li 'G'   Use the shorter of E or f
    \endlist

    If precision is not specified, the precision will be 2.

    If the format is not specified 'f' will be used.

    If \a locale is not specified, the default locale will be used.

    The following example shows a number formatted for the German locale:
    \qml
    import QtQuick 2.0

    Text {
        text: "The value is: " +  Number(4742378.423).toLocaleString(Qt.locale("de_DE"))
    }
    \endqml

    You can customize individual fields of the \a{locale} to tightly control
    the output:
    \qml
    let locale = Qt.locale("de_DE");
    let a = Number(1000).toLocaleString(locale)); // a == 1.000,00
    locale.numberOptions = Locale.OmitGroupSeparator;
    let b = Number(1000).toLocaleString(locale)); // b == 1000,00
    \endqml

    You can apply toLocaleString() directly to constants, provided the decimal
    is included in the constant, e.g.
    \qml
    123.0.toLocaleString(Qt.locale("de_DE")) // OK
    123..toLocaleString(Qt.locale("de_DE"))  // OK
    123.toLocaleString(Qt.locale("de_DE"))   // fails
    \endqml

    \sa {QtQml::Locale}{Locale}
*/

/*!
    \qmlmethod string Number::toLocaleCurrencyString(locale,symbol)

    Converts the Number to a currency using the currency and conventions of the specified
    \a locale.  If \a symbol is specified it will be used as the currency
    symbol.

    \sa Locale::currencySymbol()
*/

/*!
    \qmlmethod string Number::fromLocaleString(locale,number)

    Returns a Number by parsing \a number using the conventions of the supplied \a locale.

    If \a locale is not supplied the default locale will be used.

    For example, using the German locale:
    \qml
    var german = Qt.locale("de_DE");
    var d;
    d = Number.fromLocaleString(german, "1234,56")   // d == 1234.56
    d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
    d = Number.fromLocaleString(german, "1234.56")  // throws exception
    d = Number.fromLocaleString(german, "1.234")    // d == 1234.0
    \endqml

    \sa {QtQml::Locale}{Locale}
*/