Pergunta

I have QTableWidget with 3 columns. I multiply numbers in first and second column and write result in third column. Numbers are double. The problem is: result is in scientific notation like "1.4e+3". How can I change notation to standard notation? I use Qt 4.8 and Ubuntu 12.10.

I populate first two columns:

QTableWidgetItem *itm0=new QTableWidgetItem("12345.6781");
ui->tableWidget->setItem(0,0,itm0);
QTableWidgetItem *itm1=new QTableWidgetItem("223.132");
ui->tableWidget->setItem(0,1,itm1);

I multiply numbers and write result

double num0=ui->tableWidget->item(0,0)->text().toDouble();
double num1=ui->tableWidget->item(0,1)->text().toDouble();
double result=num0*num1;

QTableWidgetItem *itm2=new QTableWidgetItem(QString::number(result));
ui->tableWidget->setItem(0,2,itm2);

result is 2.75472e+06, but actually result is 2754715,8458092

Foi útil?

Solução

I suppose you are using QString::number function . You can use the second argument of this function to specify how the conversion from double to QString will be done. Here are the outputs :

double num = 1234.4565;
qDebug()<< QString::number(num);//"1234.46" 
qDebug()<< QString::number(num, 'e');//"1.234457e+03" 
qDebug()<< QString::number(num, 'f');//"1234.456500" 

Use
'e' format as [-]9.9e[+|-]999
'E' format as [-]9.9E[+|-]999
'f' format as [-]9.9
'g' use e or f format, whichever is the most concise
'G' use E or f format, whichever is the most concise
Assistant is my best friend ;)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top