Results 1 to 8 of 8

Thread: QString to double without exponential

  1. #1
    Join Date
    Nov 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QString to double without exponential

    I want to write "8989898.89" from a lineedit to my database. I use the code:

    Qt Code:
    1. double databasevar=0;
    2. databasevar = ui->lineEdit_input->text().toDouble();
    3. ...
    4. sql insert ...
    5. ...
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString to double without exponential

    you're probably doing something wrong that you are not showing us. Read my sig.


    Qt Code:
    1. if __name__ == '__main__':
    2. app = QApplication(sys.argv)
    3.  
    4. le = QLineEdit()
    5. le.show()
    6.  
    7. btn = QPushButton("click")
    8. btn.show()
    9. def printText():
    10. print le.text()
    11. print float(le.text())
    12.  
    13. btn.clicked.connect(printText)
    14. app.exec_()
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. 898989.55
    2. 898989.55
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString to double without exponential

    Qt Code:
    1. double d;
    2. d = QString( "1234.56e-02" ).toDouble();
    3. qDebug() << d;
    To copy to clipboard, switch view to plain text mode 
    This should give : 12.3456
    So whats your problem exactly ?
    Last edited by toufic.dbouk; 15th October 2013 at 00:38.

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString to double without exponential

    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:
    Qt Code:
    1. double x = 123423.3;
    2. QString toDisplay = QString::number(x, 'f', 1);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString to double without exponential

    Database value 123456.78
    my code to read the value

    Qt Code:
    1. bool Vertrag_h::sql_get(QString vertragID){
    2. q.exec(QString("SELECT * FROM vertrag WHERE vertragid='%1' LIMIT 1").arg(vertragID));
    3. if(!q.isActive()){
    4. qDebug() << "SQL Error:"+q.lastError().text();
    5. return 0;
    6. }else{
    7. if(q.size()>0){
    8. while(q.next()){
    9. this->vertragid = q.value(0).toString();
    10. this->kaufpreis1 = q.value(1).toDouble();
    11. }
    12. return 1;
    13. }else{
    14. return 0;
    15. }
    16. }
    17. }
    18.  
    19. bool Vertrag_h::sql_set(){
    20. q.exec(QString("UPDATE Vertrag_h SET kaufpreis1='%2' WHERE vertragid='%1'").arg(this->vertragid).arg(this->kaufpreis1));
    21. if(!q.isActive()){
    22. qDebug() << "SQL Error:"+q.lastError().text();
    23. return 0;
    24. }else{
    25. return 1;
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    and i lost my last digits.

  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString to double without exponential

    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.
    Last edited by MarekR22; 15th October 2013 at 12:24.

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString to double without exponential

    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.

  8. #8
    Join Date
    Sep 2017
    Posts
    23
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString to double without exponential

    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 :
    Qt Code:
    1. [static] QString QString::number(double n, char format = 'g', int precision = 6)
    To copy to clipboard, switch view to plain text mode 
    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

Similar Threads

  1. Exponential calculation
    By bollibompa in forum Newbie
    Replies: 1
    Last Post: 4th April 2011, 23:11
  2. QString to double convertion problem
    By NoRulez in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2010, 18:48
  3. probleme with cast from double to QString
    By hbill in forum Qt Programming
    Replies: 7
    Last Post: 16th May 2008, 23:00
  4. Cannot convert double to QString
    By maxpower in forum Qt Programming
    Replies: 9
    Last Post: 24th December 2007, 03:04
  5. double to QString
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 22nd November 2007, 14:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.