我正在尝试将数字转换为货币字符串。我正在使用QLocale::toCurrencyString()
方法。当我将系统区域设置(通过 Windows 7 区域和语言设置更改)设置为 India 并尝试使用 转换数字1872345.00
时QLocale().toCurrencyString(1872345.00)
,我得到了预期的货币字符串。
但是当我打电话时QLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00);
,我得到这样格式的号码,这是不正确的。
我尝试选择不同的语言环境脚本和语言环境。但我无法得到正确的结果。我正在使用 Qt 5.1.0。虽然我在使用时得到了正确的结果,但QLocale().toCurrencyString()
我无法使用它,因为我正在处理多个国家的货币,而不仅仅是系统语言环境设置的国家。为什么我得到这个不正确的结果?如何将数字转换为(不同国家的)货币字符串?
这是一个示例代码。
//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"
}
}
}
//结果
这里我的默认系统区域设置为印地语印度(通过 Windows 7 区域和语言设置)