L
L
Lisik2019-09-09 00:10:35
Qt
Lisik, 2019-09-09 00:10:35

How to set the number of decimal places in QT?

I suspect that the question is stupid, but I did not find the answer to a specific question on the forums. QListWidget is used as the response field.

...
x = x + zkx;
y = y + zky;
Z = sqrt(pow(xx - x, 2) + pow(yy - y, 2));
        }
        ui->answer->addItem(QString::fromStdString("x: " + std::to_string(x) + " y: " + std::to_string(y)));
}

How can I make sure that "x" and "y" are displayed with the given precision in the answer?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-09-09
@Lisik

double x = 12.3456789;
double y = 98.7654321;
QString str = QString("x: %1 y: %2").arg(x, 0, 'f', 3).arg(y, 0, 'f', 3);

OUT:
x: 12.346 y: 98.765

https://doc.qt.io/qt-5/qstring.html#arg-9
https://doc.qt.io/qt-5/qstring.html#argument-formats

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question