我正在尝试将数字转换为货币字符串。我正在使用QLocale::toCurrencyString()
方法。
当我将我的系统区域设置(通过Windows 7区域和语言设置更改)设置为印度并尝试使用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区域和语言设置)
答案 0 :(得分:0)
对我来说这看起来像个错误;但是,如果要在默认情况下验证系统区域设置正在使用的值,可以尝试:
qDebug() << QLocale().languageToString(QLocale().language());
qDebug() << QLocale().scriptToString(QLocale().script());
我的节目&#34;默认&#34;作为脚本,但如果我这样做:
qDebug() << QLocale().script();
我得到&#34; 0&#34;,文档说这是QLocale :: AnyScript的价值。我要说的是,如果你默认得到了正确的东西,但是在ctor中提供相同的语言,脚本和国家,就会出现问题。
您可能需要暂时对其进行编码,和/或将其放入错误报告中。