我正在尝试将数字转换为货币字符串。我使用的是QLocale::toCurrencyString()方法。当我将系统区域设置(通过Windows7区域和语言设置更改)设置为印度,并尝试使用QLocale().toCurrencyString(1872345.00)转换数字(例如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区域和语言设置)
发布于 2016-09-01 03:43:25
在我看来这是一个bug;但是如果您想验证您的系统区域设置默认使用的值,您可以尝试:
qDebug() << QLocale().languageToString(QLocale().language());
qDebug() << QLocale().scriptToString(QLocale().script());我的脚本显示为"Default“,但如果我这样做:
qDebug() << QLocale().script();我得到"0",文档说它是QLocale::AnyScript的值。我会说,如果你默认得到了正确的东西,但是在ctor中提供了相同的语言、脚本和国家,那么就有问题了。
你可能不得不暂时绕过它进行编码,和/或提交一个错误报告。
https://stackoverflow.com/questions/23452024
复制相似问题