PDA

View Full Version : How to display a double?



TomASS
29th May 2009, 16:09
Hello, I would like to display a double number into a MessageBox:

if I write:

double d = 0.75;
QString str = QString("delta: %1").arg(d, 0, 'g', 6);
QMessageBox::information(0,"Time","Value: "+str+".\n");
i's display ok: Value: 0.75.

But if I write:

double d = 3/4;
QString str = QString("delta: %1").arg(d, 0, 'g', 6);
QMessageBox::information(0,"Time","Value: "+str+".\n");
i's display wrong: Value: 0.. Why and what I should write to display Value: 0.75?

vcp
29th May 2009, 17:50
Hi,

You can use this code:

QString("%L0").arg(v,0,'f',2)

***Note the use of 'f' parameter.***

for example:

double v = 150.63;

QString qsValue = QString("%L0").arg(v,0,'f',2);

qDebug() << qsValue;

wysota
29th May 2009, 18:24
But if I write:

double d = 3/4;
QString str = QString("delta: %1").arg(d, 0, 'g', 6);
QMessageBox::information(0,"Time","Value: "+str+".\n");
i's display wrong: Value: 0.. Why and what I should write to display Value: 0.75?

3/4 is 0. If you divide two integers the result is also an integer.

LeNsTR
29th May 2009, 20:15
Try it:


double d = 3.0 / 4;