我在验证价格时遇到麻烦。
示例接受价格: 10,00 / 100,00 / 1.000,00
不接受: 10 / 100 / 1000.00
代码,但这通过了100 / 10 / 1000.00
bool ok;
QLocale::setDefault(QLocale(QLocale::Portuguese, QLocale::Brazil));
QLocale brazil; // Constructs a default QLocale
QString text;
if(ui->price->text().length() <= 2){
qDebug() << text.sprintf("%6.2f", ui->price->text().toDouble()); //format 50 = 50.00
}
brazil.toDouble(ui->price->text(), &ok);
qDebug() << ok;发布于 2014-03-17 23:49:10
正如你所预期的,Qt4.8和最新的Qt5验证都失败了。检查你是否可以在你的平台上正确地设置QLocale。brazil.decimalPoint()应返回',‘
注意:如果你想要语言环境感知的格式,你不能使用QString::sprintf。显式使用QTextStream和setLocale(),因为它将默认使用C语言环境。
#include <QLocale>
#include <QDebug>
#include <QStringList>
int main()
{
bool ok;
QLocale::setDefault(QLocale(QLocale::Portuguese, QLocale::Brazil));
QLocale brazil; // Constructs a default QLocale
QStringList textlist = QStringList() << "400.00" << "400" << "400,00";
for (QString text : textlist) {
brazil.toDouble(text, &ok);
qDebug() << text << "is" << ok;
}
return 0;
}收益率,
"400.00" is false
"400" is true
"400,00" is truehttps://stackoverflow.com/questions/22454552
复制相似问题