PDA

View Full Version : QString to double without exponential



binio
15th October 2013, 00:45
I want to write "8989898.89" from a lineedit to my database. I use the code:



double databasevar=0;
databasevar = ui->lineEdit_input->text().toDouble();
...
sql insert ...
...


after the insert i have in my database the value "8989900.00" -> "8.9899e+06".
how do i deactivate the exponential translation that kills the last digits.

Can someone help me pls

amleto
15th October 2013, 01:25
you're probably doing something wrong that you are not showing us. Read my sig.




if __name__ == '__main__':
app = QApplication(sys.argv)

le = QLineEdit()
le.show()

btn = QPushButton("click")
btn.show()
def printText():
print le.text()
print float(le.text())

btn.clicked.connect(printText)
app.exec_()



898989.55
898989.55

toufic.dbouk
15th October 2013, 01:27
double d;
d = QString( "1234.56e-02" ).toDouble();
qDebug() << d;
This should give : 12.3456
So whats your problem exactly ?

MarekR22
15th October 2013, 11:11
Problem is that you think that it is a database problem. Database doesn't store the format of number it stores the value (and type of column of course).
Your code should handle proper number formatting when you are display that value from database. For example:

double x = 123423.3;
QString toDisplay = QString::number(x, 'f', 1);

binio
15th October 2013, 11:27
Database value 123456.78
my code to read the value


bool Vertrag_h::sql_get(QString vertragID){
QSqlQuery q;
q.exec(QString("SELECT * FROM vertrag WHERE vertragid='%1' LIMIT 1").arg(vertragID));
if(!q.isActive()){
qDebug() << "SQL Error:"+q.lastError().text();
return 0;
}else{
if(q.size()>0){
while(q.next()){
this->vertragid = q.value(0).toString();
this->kaufpreis1 = q.value(1).toDouble();
}
return 1;
}else{
return 0;
}
}
}

bool Vertrag_h::sql_set(){
QSqlQuery q;
q.exec(QString("UPDATE Vertrag_h SET kaufpreis1='%2' WHERE vertragid='%1'").arg(this->vertragid).arg(this->kaufpreis1));
if(!q.isActive()){
qDebug() << "SQL Error:"+q.lastError().text();
return 0;
}else{
return 1;
}
}

and i lost my last digits.

MarekR22
15th October 2013, 13:00
What is type of your `kaufpreis1` column? Looks like it is a TEXT not a DECIMAL or FLOAT (your query proves that, you have apostrophes there).
So when you put values into database you are loosing precision by default conversion to string and then you are fetching string value which is converted to double.

edit:
now I see that it is even worse! Never ever put data into query like this (this way you are vulnerable for SQL injection).
Read this (http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html#approaches-to-binding-values).

Lesiok
15th October 2013, 13:26
First of all no database does not guarantee the immutable order of the data by the query SELECT * FROM ...
Use an explicit specification of columns and then you can safely use the number of columns in the result or read data using column names.

Ahmed Abdellatif
12th April 2018, 12:07
Searching for that problem, i have found the following 2 links that will be helpful for those who will search about this in the future :
1. From QT help :

[static] QString QString::number(double n, char format = 'g', int precision = 6)
Returns a string equivalent of the number n, formatted according to the specified format and precision. See Argument Formats for details.

2. http://doc.qt.io/qt-5/qstring.html#argument-formats.
Read them carefully, i think it is adequate