简体   繁体   中英

Convert QString/QByteArray to Scientific Notation qt

Is there a way to easily convert a QByteArray or QString or int displaying some large integer to scientific notation? I need to display it as a string in a QLineEdit .

I'm sure this has already been asked, but I haven't found what I'm looking for.

Convert QByteArray : 475000000 to QString : 4.75E8

QString aaa("475000000");
ui->lineEdit->setText(QString::number(aaa.toDouble()));

Output:

4.75e+08

Or

QString aaa("475000000");
QString formatted = QString::number(aaa.toDouble()).remove("+");
ui->lineEdit->setText(formatted.toUpper());

Output:

4.75E08

QString aaa("475000000");
QString formatted = QString::number(aaa.toDouble()).remove("+");
formatted.replace("e0","E");
ui->lineEdit->setText(formatted.toUpper());

Output:

4.75E8

The easiest choice is to convert string to number and then number to string.
There are many possibilities:

QString aaa("475000000");
double value = aaa.toDouble();
QString cStyleResult = QString::number(value, 'e');
QString localeAwareResult = QLocale::system().toString(value, 'e');
QString noLocaleFormat = QString("value=%1").arg(value, 0, 'e');
QString localeAwareFormat = QString("value=%L1").arg(value, 0, 'e');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM