Converting number to currency string using QLocale class

4.1k Views Asked by At

I am trying to convert numbers into currency string. I am using QLocale::toCurrencyString() method. When I set my system locale(changed via Windows 7 Region and Language setup) as India and try to convert a number say 1872345.00 using QLocale().toCurrencyString(1872345.00), I get the expected currency string.

QLocale().toCurrencyString(1872345.00)

But when I call QLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00);, I get the number formatted like this which is not correct.

QLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00)

I tried choosing different locale Scripts and locale Languages. But I was not able to get the right result. I am using Qt 5.1.0. Though I am getting right results when I use QLocale().toCurrencyString() I cannot use it because I am dealing with currencies of multiple countries and not just the country as set by system locale. Why is that I am getting this incorrect result? How do I convert numbers to currency string (of different countries)?

Here's an example code.

//CurrencyForms.h

#ifndef CURRENCYFORMS_H
#define CURRENCYFORMS_H

#include <QObject>

class CurrencyForms : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString defaultLocaleString READ defaultLocaleString CONSTANT)
    Q_PROPERTY(QString constructedLocaleStringAnyLanguage READ constructedLocaleStringAnyLanguage CONSTANT)
    Q_PROPERTY(QString constructedLocaleStringHindiLanguage READ constructedLocaleStringHindiLanguage CONSTANT)
public:
    explicit CurrencyForms(QObject *parent = 0);
    QString defaultLocaleString();
    QString constructedLocaleStringAnyLanguage();
    QString constructedLocaleStringHindiLanguage();
};

#endif // CURRENCYFORMS_H

//CurrencyForms.cpp

#include "CurrencyForms.h"
#include<QLocale>
CurrencyForms::CurrencyForms(QObject *parent) :
    QObject(parent)
{
}

QString CurrencyForms::defaultLocaleString()
{
    return QLocale().toCurrencyString(1872345.00);
}

QString CurrencyForms::constructedLocaleStringAnyLanguage()
{
    return QLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00);
}

QString CurrencyForms::constructedLocaleStringHindiLanguage()
{
    return  QLocale(QLocale::Hindi,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00);
}

//main.cpp

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>
#include <CurrencyForms.h>
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    CurrencyForms forms;
    viewer.rootContext()->setContextProperty("forms",&forms);
    viewer.setMainQmlFile(QStringLiteral("qml/CurrencySymbolTest/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

//main.qml

import QtQuick 2.0

Rectangle {
    width: 720
    height: 360
    Grid{
        columns:1
        spacing:2
        width: parent.width
        Text{
            id:defaultLocaleText
            width:parent.width
            height:60
            font.pixelSize:14
            elide:Text.ElideRight
            text:"QLocale().toCurrencyString(1872345.00)"
            verticalAlignment: Text.AlignVCenter
        }
        Text{
            id:defaultLocaleTextResult
            width:parent.width
            height:60
            font.pixelSize:40
            elide:Text.ElideRight
            text:forms.defaultLocaleString
            verticalAlignment: Text.AlignVCenter
            color:"green"
        }
        Text{
            id:constructedLocaleStringAnyLanguageText
            width:parent.width
            height:60
            font.pixelSize:14
            elide:Text.ElideRight
            text:"QLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00)"
            verticalAlignment: Text.AlignVCenter
        }
        Text{
            id:constructedLocaleStringAnyResult
            width:parent.width
            height:60
            font.pixelSize:40
            elide:Text.ElideRight
            text:forms.constructedLocaleStringAnyLanguage
            verticalAlignment: Text.AlignVCenter
            color:"red"
        }
        Text{
            id:constructedLocaleStringHindiLanguage
            width:parent.width
            height:60
            font.pixelSize:14
            elide:Text.ElideRight
            text:"QLocale(QLocale::Hindi,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00)"
            verticalAlignment: Text.AlignVCenter
        }
        Text{
            id:constructedLocaleStringHindiLanguageResult
            width:parent.width
            height:60
            font.pixelSize:40
            elide:Text.ElideRight
            text:forms.constructedLocaleStringHindiLanguage
            verticalAlignment: Text.AlignVCenter
            color:"red"
        }

    }

}

//Result enter image description here

here my default system locale is set as Hindi India (via Windows 7 Region and Language setup)

1

There are 1 best solutions below

0
On

It looks like a bug to me; but if you want to verify the values your system locale is using by default, you can try:

qDebug() << QLocale().languageToString(QLocale().language());
qDebug() << QLocale().scriptToString(QLocale().script());

Mine shows "Default" as the script, but if I do:

qDebug() << QLocale().script();

I get "0", which the docs say is the value of QLocale::AnyScript. I'd say, if you get the right thing by default, but supply the same language, script and country in the ctor, there's something wrong.

You might have to code around it for the time being, and/or put in a bug report.