Don't rely on the default behaviour of QVariant::toString() if you don't like its output. As a variation to borisbn's approach, you could use QString::number() and choose the 'f' format and a number desired figures after the decimal point.
BTW: If you want to convert to a double why do you convert to a QVariant?
lineEdit->setText("12345678");
bool ok;
double d = lineEdit->text().toDouble(&ok);
if (ok) {
qDebug
() <<
QString::number(d,
'f',
6);
qDebug
() <<
QString::number(d,
'f',
2);
qDebug
() <<
QString::number(d,
'f',
0);
}
lineEdit->setText("12345678");
bool ok;
double d = lineEdit->text().toDouble(&ok);
if (ok) {
qDebug() << QString::number(d, 'f', 6);
qDebug() << QString::number(d, 'f', 2);
qDebug() << QString::number(d, 'f', 0);
}
To copy to clipboard, switch view to plain text mode
Bookmarks