427 changes: 427 additions & 0 deletions src/corelib/tools/qnumberformatter.cpp

Large diffs are not rendered by default.

307 changes: 307 additions & 0 deletions src/corelib/tools/qnumberformatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
/****************************************************************************
**
** Copyright (C) 2013 John Layt jlayt@kde.org
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QNUMBERFORMATTER_H
#define QNUMBERFORMATTER_H

#include "qlocale.h"

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

class QNumberFormatterPrivate;

class Q_CORE_EXPORT QNumberFormatter
{
public:
// TODO Move these enums to QLocale?

// Mac still to integrate:
// negativeFormat
// positiveFormat
// usesGroupingSeparator
// alwaysShowsDecimalSeparator
// allowsFloats
// minimum
// maximum
// isPartialStringValidationEnabled

// Pre-defined number styles
// ICU UNumberFormatStyle
// Mac NSNumberFormatterStyle
enum NumberStyle {
// ICU and Mac
DecimalStyle = 1, // 10.00
ScientificStyle, // 1.0E2
CurrencyStyle, // $10.00
PercentageStyle, // 10%
SpelloutStyle, // ten
DecimalPatternStyle, // User defined pattern and symbols
// TODO Check on Mac
RuleBasedStyle, // User defined rules
// ICU Only
OrdinalStyle, // 10th
DurationStyle, // 00:00:10
// ICU >= 4.8 Only
CurrencyCodeStyle, // USD10.00
CurrencyNameStyle, // 10 US dollars
LastNumberStyle = CurrencyNameStyle
};

// String Symbols and attributes affecting how a number is formatted
// Selected ICU UNumberFormatSymbol and UNumberTextAttribute
// Mac not an enum, uses named method calls, also has nilSymbol and negativeInfinitySymbol

enum NumberSymbol {
// ICU and Mac
DecimalSeparatorSymbol = 1,
GroupingSeparatorSymbol,
PercentSymbol,
MinusSignSymbol,
PlusSignSymbol,
ExponentialSymbol,
PerMillSymbol,
InfinitySymbol,
NaNSymbol,
CurrencySymbol,
CurrencyInternationalSymbol,
MonetaryDecimalSeparatorSymbol,
MonetaryGroupingSeparatorSymbol,
// ICU only
PatternSeparatorSymbol,
PatternDigitSymbol,
PatternSignificantDigitSymbol,
PatternPadEscapeSymbol,
PatternFormatSymbolCount,
// ICU > 4.6 and Mac
ZeroDigitSymbol,
// ICU > 4.6 only
OneDigitSymbol,
TwoDigitSymbol,
ThreeDigitSymbol,
FourDigitSymbol,
FiveDigitSymbol,
SixDigitSymbol,
SevenDigitSymbol,
EightDigitSymbol,
NineDigitSymbol
};

// Integer Attibutes affecting how a number is formatted
// String, bool, enum, and double attributes are handled separately
// ICU UNumberAttribute
enum NumberAttribute {
MaximumIntegerDigits = 1,
MinimumIntegerDigits,
MaximumFractionDigits,
MinimumFractionDigits,
MaximumSignificantDigits,
MinimumSignificantDigits,
Multiplier,
PrimaryGroupingSize,
SecondaryGroupingSize
};

// Rounding modes
// ICU UNumberFormatRoundingMode
// Mac NSNumberFormatterRoundingMode
enum RoundingMode {
DefaultRoundingMode = 0,
RoundCeiling,
RoundFloor,
RoundDown,
RoundUp,
RoundHalfEven,
RoundHalfDown,
RoundHalfUp,
// ICU only
RoundUnnecessary
};

// ICU UNumberFormatPadPosition
// Mac NSNumberFormatterPadPosition
enum PadPosition {
DefaultPadPosition = 0,
PadBeforePrefix,
PadAfterPrefix,
PadBeforeSuffix,
PadAfterSuffix
};

// Create default decimal formatter in default locale
QNumberFormatter();
// Create named style formatter using named locale
// TODO change localeCode QString to QLocaleCode?
QNumberFormatter(NumberStyle style, const QString &localeCode = QString());
~QNumberFormatter();

QNumberFormatter(const QNumberFormatter &other);
QNumberFormatter &operator=(const QNumberFormatter &other);

static QNumberFormatter system(NumberStyle style = DecimalStyle);

bool isValid() const;

// TODO change localeCode QString to QLocaleCode?
QString localeCode(QLocale::LocaleCodeType type = QLocale::RequestedLocaleCode) const;
NumberStyle numberStyle() const;
static QList<QNumberFormatter::NumberStyle> availableNumberStyles();
QString currencyCode() const;

QString numberSymbol(NumberSymbol symbol) const;
qint32 numberAttribute(NumberAttribute attribute) const;
bool usesSignificantDigits() const;

// Number Rounding settings
RoundingMode roundingMode() const;
double roundingIncrement() const;

// Number Padding settings
qint32 paddingWidth() const;
QString padding() const;
PadPosition paddingPosition() const;

QLocale::ParsingMode parsingMode() const;

// Uses locale default settings
QString toString(qint32 from) const;
QString toString(qint64 from) const;
QString toString(double from) const;

// Override padding only
QString toString(qint32 from, int width, const QString &padding = QString(),
PadPosition position = DefaultPadPosition) const;
QString toString(qint64 from, int width, const QString &padding = QString(),
PadPosition position = DefaultPadPosition) const;
QString toString(double from, int width, const QString &padding = QString(),
PadPosition position = DefaultPadPosition) const;

// Override Int/Frac digits and optionally rounding mode
QString toString(double from,
int maxIntDigits, int minIntDigits, int maxFracDigits, int MinFracDigits,
RoundingMode mode = DefaultRoundingMode,
double roundIncrement = -1.0) const;

// Override significant digits and optionally rounding mode
QString toString(double from,
int maxSigDigits, int minSigDigits,
RoundingMode mode = DefaultRoundingMode,
double roundIncrement = -1.0) const;

// Override all Int/Frac options
QString toString(double from,
int maxIntDigits, int minIntDigits, int maxFracDigits, int MinFracDigits,
int width = -1, const QString &padding = QString(),
PadPosition position = DefaultPadPosition,
RoundingMode mode = DefaultRoundingMode, double roundIncrement = -1.0) const;

// Override all significant digits options
QString toString(double from,
int maxSigDigits, int minSigDigits,
int width = -1, const QString &padding = QString(),
PadPosition position = DefaultPadPosition,
RoundingMode mode = DefaultRoundingMode, double roundIncrement = -1.0) const;

// Uses locale default settings
qint32 toInt32(const QString &from, bool *ok = 0,
QLocale::ParsingMode mode = QLocale::DefaultParsingMode) const;
qint64 toInt64(const QString &from, bool *ok = 0,
QLocale::ParsingMode mode = QLocale::DefaultParsingMode) const;
double toDouble(const QString &from, bool *ok = 0,
QLocale::ParsingMode mode = QLocale::DefaultParsingMode) const;

// Format/Parse any currency using this locale settings, only if NumberFormat is Currency type
// Format will use the correct number of decimal places for currency code given
QString toAnyCurrency(double from, const QString &currencyCode,
int width = -1, const QString &padding = QString(),
PadPosition position = DefaultPadPosition,
RoundingMode mode = DefaultRoundingMode, double roundIncrement = -1.0) const;
double fromAnyCurrency(const QString &from, QString *currencyCode, bool *ok = 0,
QLocale::ParsingMode mode = QLocale::DefaultParsingMode) const;

private:
// Create locale with backend
QNumberFormatter(QNumberFormatterPrivate &dd);
friend class QNumberFormatterPrivate;
friend class QCustomNumberFormatter;
QSharedDataPointer<QNumberFormatterPrivate> d;
};

class Q_CORE_EXPORT QCustomNumberFormatter : public QNumberFormatter
{
public:
// Create named style in named locale
QCustomNumberFormatter(QNumberFormatter::NumberStyle style,
const QString &localeCode = QString());
// Create named locale in named locale using custom pattern or rules
QCustomNumberFormatter(QNumberFormatter::NumberStyle style, const QString &patternOrRules,
const QString &localeCode = QString());
~QCustomNumberFormatter();

QCustomNumberFormatter(const QCustomNumberFormatter &other);
QCustomNumberFormatter &operator=(const QCustomNumberFormatter &other);

// Clone the other/system/default nf and return custom version
QCustomNumberFormatter(const QNumberFormatter &other);
QCustomNumberFormatter &operator=(const QNumberFormatter &other);
static QCustomNumberFormatter system(NumberStyle style = DecimalStyle);

void setCurrencyCode(const QString & currencyCode);

QString pattern() const;
void setPattern(const QString & pattern);

void setNumberSymbol(QNumberFormatter::NumberSymbol symbol, const QString &value);
void setNumberAttribute(QNumberFormatter::NumberAttribute attribute, qint32 value);
void setUsesSignificantDigits(bool value);

void setRounding(QNumberFormatter::RoundingMode mode, double increment = -1.0);
void setPadding(qint32 width, const QString &padding,
QNumberFormatter::PadPosition position = DefaultPadPosition);

void setParsingMode(QLocale::ParsingMode mode);
};

QT_END_NAMESPACE

QT_END_HEADER

#endif // QNUMBERFORMATTER_H
439 changes: 439 additions & 0 deletions src/corelib/tools/qnumberformatterprivate.cpp

Large diffs are not rendered by default.

1,054 changes: 1,054 additions & 0 deletions src/corelib/tools/qnumberformatterprivate_icu.cpp

Large diffs are not rendered by default.

1,048 changes: 1,048 additions & 0 deletions src/corelib/tools/qnumberformatterprivate_mac.cpp

Large diffs are not rendered by default.

674 changes: 674 additions & 0 deletions src/corelib/tools/qnumberformatterprivate_mac.mm

Large diffs are not rendered by default.

173 changes: 173 additions & 0 deletions src/corelib/tools/qnumberformatterprivate_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/****************************************************************************
**
** Copyright (C) 2012 John Layt jlayt@kde.org
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QNUMBERFORMATTERPRIVATE_P_H
#define QNUMBERFORMATTERPRIVATE_P_H

//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of internal files. This header file may change from version to version
// without notice, or even be removed.
//
// We mean it.
//

#include "qnumberformatter.h"

#ifdef Q_OS_MAC
#ifdef __OBJC__
@class NSNumberFormatter;
#else
class NSNumberFormatter;
#endif // __OBJC__
#endif // Q_OS_MAC

#ifdef QT_USE_ICU
#include "unicode/unum.h"
#endif // QT_USE_ICU

QT_BEGIN_NAMESPACE

class QNumberFormatterPrivate : public QSharedData
{
public:
QNumberFormatterPrivate(QNumberFormatter::NumberStyle style, const QString &patternOrRules,
const QString &localeCode);
QNumberFormatterPrivate(const QNumberFormatterPrivate &other);
~QNumberFormatterPrivate();

virtual QNumberFormatterPrivate *clone();

bool isValid() const;

QString localeCode(QLocale::LocaleCodeType type = QLocale::RequestedLocaleCode) const;

QNumberFormatter::NumberStyle numberStyle() const;
static QList<QNumberFormatter::NumberStyle> availableNumberStyles();

QString pattern() const;
void setPattern(const QString & pattern);

QString currencyCode() const;
void setCurrencyCode(const QString & currencyCode);

QString numberSymbol(QNumberFormatter::NumberSymbol symbol) const;
void setNumberSymbol(QNumberFormatter::NumberSymbol symbol, const QString &value);

qint32 numberAttribute(QNumberFormatter::NumberAttribute attribute) const;
void setNumberAttribute(QNumberFormatter::NumberAttribute attribute, qint32 value);

bool usesSignificantDigits() const;
void setUsesSignificantDigits(bool value);

QLocale::ParsingMode parsingMode() const;
void setParsingMode(QLocale::ParsingMode mode);

QNumberFormatter::RoundingMode roundingMode() const;
double roundingIncrement() const;
void setRounding(QNumberFormatter::RoundingMode mode, double increment);

qint32 paddingWidth() const;
QString padding() const;
QNumberFormatter::PadPosition paddingPosition() const;
void setPadding(qint32 width, const QString &padding,
QNumberFormatter::PadPosition position);

QString toString(qint32 from) const;
QString toString(qint64 from) const;
QString toString(double from) const;

QString toString(qint32 from, int width, const QString &padding,
QNumberFormatter::PadPosition position) const;
QString toString(qint64 from, int width, const QString &padding,
QNumberFormatter::PadPosition position) const;
QString toString(double from, int width, const QString &padding,
QNumberFormatter::PadPosition position) const;

QString toString(double from,
int maxIntDigits, int minIntDigits, int maxFracDigits, int MinFracDigits,
QNumberFormatter::RoundingMode mode, double increment) const;

QString toString(double from,
int maxSigDigits, int minSigDigits,
QNumberFormatter::RoundingMode mode, double increment) const;

QString toString(double from,
int maxIntDigits, int minIntDigits, int maxFracDigits, int MinFracDigits,
int width, const QString &padding, QNumberFormatter::PadPosition position,
QNumberFormatter::RoundingMode mode, double increment) const;

QString toString(double from,
int maxSigDigits, int minSigDigits,
int width, const QString &padding, QNumberFormatter::PadPosition position,
QNumberFormatter::RoundingMode mode, double increment) const;

qint32 toInt32(const QString &from, bool *ok, QLocale::ParsingMode mode) const;
qint64 toInt64(const QString &from, bool *ok, QLocale::ParsingMode mode) const;
double toDouble(const QString &from, bool *ok, QLocale::ParsingMode mode) const;

QString toAnyCurrency(double from, const QString &currencyCode,
int width, const QString &padding, QNumberFormatter::PadPosition position,
QNumberFormatter::RoundingMode mode, double increment) const;
double fromAnyCurrency(const QString &from, QString *currencyCode, bool *ok,
QLocale::ParsingMode mode) const;

private:
QNumberFormatter::NumberStyle m_style;
QString m_localeCode;

#ifdef QT_USE_ICU
UNumberFormat *m_nf;
UErrorCode m_status;
#endif // QT_USE_ICU

#ifdef Q_OS_MAC
NSNumberFormatter *m_nf;
#endif // Q_OS_MAC
};

template<> QNumberFormatterPrivate *QSharedDataPointer<QNumberFormatterPrivate>::clone();

QT_END_NAMESPACE

#endif // QNUMBERFORMATTERPRIVATE_P_H
15 changes: 13 additions & 2 deletions src/corelib/tools/tools.pri
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,22 @@ SOURCES += \
tools/qvsnprintf.cpp

!nacl:mac: {
HEADERS += tools/qnumberformatter.h \
tools/qnumberformatterprivate_p.h
SOURCES += tools/qelapsedtimer_mac.cpp
OBJECTIVE_SOURCES += tools/qlocale_mac.mm \
tools/qnumberformatterprivate_mac.mm \
tools/qtimezoneprivate_mac.mm \
tools/qstring_mac.mm
}
else:blackberry {
SOURCES += tools/qelapsedtimer_unix.cpp tools/qlocale_blackberry.cpp tools/qtimezoneprivate_tz.cpp
HEADERS += tools/qlocale_blackberry.h
SOURCES += tools/qelapsedtimer_unix.cpp \
tools/qlocale_blackberry.cpp \
tools/qnumberformatterprivate_icu.cpp \
tools/qtimezoneprivate_tz.cpp
HEADERS += tools/qlocale_blackberry.h \
tools/qnumberformatter.h \
tools/qnumberformatterprivate_p.h
}
else:unix:SOURCES += tools/qelapsedtimer_unix.cpp tools/qlocale_unix.cpp tools/qtimezoneprivate_tz.cpp
else:win32 {
Expand All @@ -142,7 +150,10 @@ contains(QT_CONFIG, zlib) {
}

contains(QT_CONFIG,icu) {
HEADERS += tools/qnumberformatter.h \
tools/qnumberformatterprivate_p.h
SOURCES += tools/qlocale_icu.cpp \
tools/qnumberformatterprivate_icu.cpp \
tools/qcollator_icu.cpp \
tools/qtimezoneprivate_icu.cpp
DEFINES += QT_USE_ICU
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CONFIG += testcase parallel_test
TARGET = tst_qnumberformatter
QT = core-private testlib
SOURCES = tst_qnumberformatter.cpp
263 changes: 263 additions & 0 deletions tests/auto/corelib/tools/qnumberformatter/tst_qnumberformatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
/****************************************************************************
**
** Copyright (C) 2012 John Layt jlayt@kde.org
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>
#include <math.h>
#include <float.h>

#include <qglobal.h>
#include <qlocale.h>
#include <qnumeric.h>
#include <private/qnumberformatter_p.h>

class tst_QNumberFormatter : public QObject
{
Q_OBJECT

public:
tst_QNumberFormatter();

private slots:
void initTestCase();
};

tst_QNumberFormatter::tst_QNumberFormatter()
{
}

void tst_QNumberFormatter::initTestCase()
{
qDebug() << "";
qDebug() << "USA";
QNumberFormatter enus(QNumberFormatter::DecimalStyle, "en_US");
qDebug() << "en_US isValid()" << enus.isValid();
qDebug() << "en_US isInvalid()" << enus.isInvalid();
qDebug() << "en_US localeCode()" << enus.localeCode();
qDebug() << "en_US localeCode(EffectiveLocaleCode)" << enus.localeCode(QLocale::EffectiveLocaleCode);
qDebug() << "en_US numberStyle()" << enus.numberStyle();
qDebug() << "en_US currencyCode()" << enus.currencyCode();
qDebug() << "en_US numberSymbol(PercentSymbol)" << enus.numberSymbol(QNumberFormatter::PercentSymbol);
qDebug() << "en_US numberAttribute(PrimaryGroupingSize)" << enus.numberAttribute(QNumberFormatter::PrimaryGroupingSize);
qDebug() << "en_US usesSignificantDigits()" << enus.usesSignificantDigits();
qDebug() << "en_US roundingMode()" << enus.roundingMode();
qDebug() << "en_US roundingIncrement()" << enus.roundingIncrement();
qDebug() << "en_US paddingWidth()" << enus.paddingWidth();
qDebug() << "en_US padding()" << enus.padding();
qDebug() << "en_US paddingPosition()" << enus.paddingPosition();
qDebug() << "en_US parsingMode()" << enus.parsingMode();
qDebug() << "";
qDebug() << "en_US toString()" << enus.toString(12345);
qDebug() << "en_US toString()" << enus.toString(2147483647);
qDebug() << "en_US toString()" << enus.toString(-2147483647);
qDebug() << "en_US toString()" << enus.toString((qint64)9223372036854775807);
qDebug() << "en_US toString()" << enus.toString((qint64)-9223372036854775807);
qDebug() << "en_US toString()" << enus.toString(12.345);
qDebug() << "en_US toString()" << enus.toString(1.123456789);
qDebug() << "en_US toString()" << enus.toString(123456789.123456789);
qDebug() << "";
qDebug() << "en_US toString()" << enus.toString(12345, 10);
qDebug() << "en_US toString()" << enus.toString(-12345, 10);
qDebug() << "en_US toString()" << enus.toString(12345, 10, QString(" "), QNumberFormatter::PadBeforePrefix);
qDebug() << "en_US toString()" << enus.toString(-12345, 10, QString(" "), QNumberFormatter::PadBeforePrefix);
qDebug() << "en_US toString()" << enus.toString(12345, 10, QString("p"), QNumberFormatter::PadAfterPrefix);
qDebug() << "en_US toString()" << enus.toString(-12345, 10, QString("p"), QNumberFormatter::PadAfterPrefix);
qDebug() << "";
qDebug() << "en_US toString()" << enus.toString(123456789.123456789, 3, 3, 3, 3);
qDebug() << "en_US toString()" << enus.toString(123456789.123456789, 3, 3, 3, 3, 10, QString("p"), QNumberFormatter::PadAfterPrefix);
qDebug() << "en_US toString()" << enus.toString(123456789.123456789, 3, 3, 3, 3, 10, QString("p"), QNumberFormatter::PadAfterPrefix, QNumberFormatter::RoundDown, 0.01);
qDebug() << "en_US toString()" << enus.toString(123456789.123456789, 3, 3);
qDebug() << "en_US toString()" << enus.toString(123456789.123456789, 3, 3, 30, QString("p"), QNumberFormatter::PadAfterPrefix);
qDebug() << "en_US toString()" << enus.toString(123456789.123456789, 3, 3, 30, QString("p"), QNumberFormatter::PadAfterPrefix, QNumberFormatter::RoundDown, 0.01);
qDebug() << "";
qDebug() << "en_US toInt32() " << enus.toInt32("12,345");
qDebug() << "en_US toInt32() " << enus.toInt32("12.345");
qDebug() << "en_US toInt32() " << enus.toInt32("12345");
qDebug() << "en_US toInt64() " << enus.toInt64("12,345");
qDebug() << "en_US toInt64() " << enus.toInt64("12.345");
qDebug() << "en_US toInt64() " << enus.toInt64("12345");
qDebug() << "en_US toDouble()" << enus.toDouble("12,345");
qDebug() << "en_US toDouble()" << enus.toDouble("12.345");
qDebug() << "en_US toDouble()" << enus.toDouble("12345");
qDebug() << "";
qDebug() << "en_US toInt32() " << enus.toInt32("12,345", 0, QLocale::StrictParsing);
qDebug() << "en_US toInt32() " << enus.toInt32("12.345", 0, QLocale::StrictParsing);
qDebug() << "en_US toInt32() " << enus.toInt32("12345", 0, QLocale::StrictParsing);
qDebug() << "en_US toInt64() " << enus.toInt64("12,345", 0, QLocale::StrictParsing);
qDebug() << "en_US toInt64() " << enus.toInt64("12.345", 0, QLocale::StrictParsing);
qDebug() << "en_US toInt64() " << enus.toInt64("12345", 0, QLocale::StrictParsing);
qDebug() << "en_US toDouble()" << enus.toDouble("12,345", 0, QLocale::StrictParsing);
qDebug() << "en_US toDouble()" << enus.toDouble("12.345", 0, QLocale::StrictParsing);
qDebug() << "en_US toDouble()" << enus.toDouble("12345", 0, QLocale::StrictParsing);
qDebug() << "";
qDebug() << "en_US toAnyCurrency(USD)" << enus.toAnyCurrency(12.34, "USD");
qDebug() << "en_US toAnyCurrency(GBP)" << enus.toAnyCurrency(12.34, "GBP");
QString code;
double amt;
amt = enus.fromAnyCurrency("USD 12.34", &code);
qDebug() << "en_US fromAnyCurrency(USD)" << amt << code;
code.clear();
amt = enus.fromAnyCurrency("GBP 12.34", &code);
qDebug() << "en_US fromAnyCurrency(GBP)" << amt << code;
code.clear();
amt = enus.fromAnyCurrency("$ 12.34", &code);
qDebug() << "en_US fromAnyCurrency($)" << amt << code;
code.clear();
amt = enus.fromAnyCurrency("£ 12.34", &code);
qDebug() << "en_US fromAnyCurrency(£)" << amt << code;

qDebug() << "";
qDebug() << "Germany";
QNumberFormatter dede(QNumberFormatter::DecimalStyle, "de_DE");
qDebug() << "de_DE isValid()" << dede.isValid();
qDebug() << "de_DE isInvalid()" << dede.isInvalid();
qDebug() << "de_DE localeCode()" << dede.localeCode();
qDebug() << "de_DE localeCode(EffectiveLocaleCode)" << dede.localeCode(QLocale::EffectiveLocaleCode);
qDebug() << "de_DE numberStyle()" << dede.numberStyle();
qDebug() << "de_DE toString()" << dede.toString(12345);
qDebug() << "de_DE toInt32()" << dede.toInt32("12,345");
qDebug() << "de_DE toInt32()" << dede.toInt32("12.345");
qDebug() << "de_DE toInt32()" << dede.toInt32("12345");

qDebug() << "";
qDebug() << "CurrencyStyle";
QNumberFormatter enusCurr("en_US", QNumberFormatter::CurrencyStyle);
qDebug() << "en_US isValid()" << enusCurr.isValid();
qDebug() << "en_US currencyCode()" << enusCurr.currencyCode();
qDebug() << "en_US toString()" << enusCurr.toString(123.45);
qDebug() << "en_US toDouble()" << enusCurr.toDouble("$123.45");
qDebug() << "en_US toAnyCurrency(USD)" << enusCurr.toAnyCurrency(12.34, "USD");
qDebug() << "en_US toAnyCurrency(GBP)" << enusCurr.toAnyCurrency(12.34, "GBP");
code.clear();
amt = enusCurr.fromAnyCurrency("USD 12.34", &code);
qDebug() << "en_US fromAnyCurrency(USD)" << amt << code;
code.clear();
amt = enusCurr.fromAnyCurrency("GBP 12.34", &code);
qDebug() << "en_US fromAnyCurrency(GBP)" << amt << code;
code.clear();
amt = enusCurr.fromAnyCurrency("$ 12.34", &code);
qDebug() << "en_US fromAnyCurrency($)" << amt << code;
code.clear();
amt = enusCurr.fromAnyCurrency("£ 12.34", &code);
qDebug() << "en_US fromAnyCurrency(£)" << amt << code;

qDebug() << "";
qDebug() << "CurrencyCodeStyle";
QNumberFormatter enusCurrCode(QNumberFormatter::CurrencyCodeStyle, "en_US");
qDebug() << "en_US isValid()" << enusCurrCode.isValid();
qDebug() << "en_US currencyCode()" << enusCurrCode.currencyCode();
qDebug() << "en_US toString()" << enusCurrCode.toString(123.45);
qDebug() << "en_US toDouble()" << enusCurrCode.toDouble("USD 123.45");
qDebug() << "en_US toAnyCurrency(USD)" << enusCurrCode.toAnyCurrency(12.34, "USD");
qDebug() << "en_US toAnyCurrency(GBP)" << enusCurrCode.toAnyCurrency(12.34, "GBP");
code.clear();
amt = enusCurrCode.fromAnyCurrency("USD 12.34", &code);
qDebug() << "en_US fromAnyCurrency(USD)" << amt << code;
code.clear();
amt = enusCurrCode.fromAnyCurrency("GBP 12.34", &code);
qDebug() << "en_US fromAnyCurrency(GBP)" << amt << code;
code.clear();
amt = enusCurrCode.fromAnyCurrency("$ 12.34", &code);
qDebug() << "en_US fromAnyCurrency($)" << amt << code;
code.clear();
amt = enusCurrCode.fromAnyCurrency("£ 12.34", &code);
qDebug() << "en_US fromAnyCurrency(£)" << amt << code;

qDebug() << "";
qDebug() << "CurrencyNameStyle";
QNumberFormatter enusCurrName(QNumberFormatter::CurrencyNameStyle, "en_US");
qDebug() << "en_US isValid()" << enusCurrName.isValid();
qDebug() << "en_US currencyCode()" << enusCurrName.currencyCode();
qDebug() << "en_US toString()" << enusCurrName.toString(123.45);
qDebug() << "en_US toDouble()" << enusCurrName.toDouble("123.45 US dollars");
qDebug() << "en_US toAnyCurrency(USD)" << enusCurrName.toAnyCurrency(12.34, "USD");
qDebug() << "en_US toAnyCurrency(GBP)" << enusCurrName.toAnyCurrency(12.34, "GBP");
code.clear();
amt = enusCurrName.fromAnyCurrency("USD 12.34", &code);
qDebug() << "en_US fromAnyCurrency(USD)" << amt << code;
code.clear();
amt = enusCurrName.fromAnyCurrency("GBP 12.34", &code);
qDebug() << "en_US fromAnyCurrency(GBP)" << amt << code;
code.clear();
amt = enusCurrName.fromAnyCurrency("$ 12.34", &code);
qDebug() << "en_US fromAnyCurrency($)" << amt << code;
code.clear();
amt = enusCurrName.fromAnyCurrency("£ 12.34", &code);
qDebug() << "en_US fromAnyCurrency(£)" << amt << code;

qDebug() << "";
qDebug() << "PercentageStyle";
QNumberFormatter enusPerc(QNumberFormatter::PercentageStyle, "en_US");
qDebug() << "en_US isValid()" << enusPerc.isValid();
qDebug() << "en_US toString()" << enusPerc.toString(1.2345);
qDebug() << "en_US toDouble()" << enusPerc.toDouble("123.45%");

qDebug() << "";
qDebug() << "ScientificStyle";
QNumberFormatter enusSci(QNumberFormatter::ScientificStyle, "en_US");
qDebug() << "en_US isValid()" << enusSci.isValid();
qDebug() << "en_US toString()" << enusSci.toString(12345);
qDebug() << "en_US toDouble()" << enusSci.toDouble("1.2345E4");

qDebug() << "";
qDebug() << "OrdinalStyle";
QNumberFormatter enusOrd(QNumberFormatter::OrdinalStyle, "en_US");
qDebug() << "en_US isValid()" << enusOrd.isValid();
qDebug() << "en_US toString()" << enusOrd.toString(12345);
qDebug() << "en_US toInt32()" << enusOrd.toInt32("12,345th");

qDebug() << "";
qDebug() << "DurationStyle";
QNumberFormatter enusDur(QNumberFormatter::DurationStyle, "en_US");
qDebug() << "en_US isValid()" << enusDur.isValid();
qDebug() << "en_US toString()" << enusDur.toString(12345);
qDebug() << "en_US toInt32()" << enusDur.toInt32("3:25:45");

qDebug() << "";
qDebug() << "SpelloutStyle";
QNumberFormatter enusSpell(QNumberFormatter::SpelloutStyle, "en_US");
qDebug() << "en_US isValid()" << enusSpell.isValid();
qDebug() << "en_US toString()" << enusSpell.toString(12345);
qDebug() << "en_US toInt32()" << enusSpell.toInt32("twelve thousand three hundred forty-five");
}

QTEST_MAIN(tst_QNumberFormatter)

#include "tst_qnumberformatter.moc"
1 change: 1 addition & 0 deletions tests/auto/corelib/tools/tools.pro
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SUBDIRS=\
qmap \
qmargins \
qmessageauthenticationcode \
qnumberformatter \
qpair \
qpoint \
qpointf \
Expand Down