Results 1 to 4 of 4

Thread: QString to double convertion problem

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default QString to double convertion problem

    Hi @all,

    i have a QLineEdit and want the text converted to a double, so I do:
    Qt Code:
    1. lineEdit->setText("123456789");
    2. QVariant val = lineEdit->text().toDouble();
    3. qDebug() << val;
    4. .
    5. .
    6. .
    7. lineEdit2->setText(val.toString());
    To copy to clipboard, switch view to plain text mode 
    Now the output would be: QVariant(double, 1.23457e+08)
    And also the value in lineEdit2 is "1.23457e+08"

    How can I avoid the exponentials?

    Thanks in advance

    Best Regards
    NoRulez

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString to double convertion problem

    Qt Code:
    1. lineEdit->setText("123456789");
    2. QVariant val = lineEdit->text().toDouble();
    3. qDebug() << val; // <--- QDebug::operator<< can't format doubles as 123.45
    4. .
    5. .
    6. .
    7. const int precision = 2;
    8. lineEdit2->setText( QString( "%1 ).arg( val.toDouble(), 0, 'f', precision ) );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString to double convertion problem

    Don't rely on the default behaviour of QVariant::toString() if you don't like its output. As a variation to borisbn's approach, you could use QString::number() and choose the 'f' format and a number desired figures after the decimal point.
    BTW: If you want to convert to a double why do you convert to a QVariant?

    Qt Code:
    1. lineEdit->setText("12345678");
    2. bool ok;
    3. double d = lineEdit->text().toDouble(&ok);
    4. if (ok) {
    5. qDebug() << QString::number(d, 'f', 6);
    6. qDebug() << QString::number(d, 'f', 2);
    7. qDebug() << QString::number(d, 'f', 0);
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to ChrisW67 for this useful post:

    waynew (28th August 2010)

  5. #4
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QString to double convertion problem

    @ChrisW67: Because the QVariant is in my example a model

Similar Threads

  1. Convertion between QLineEdit formated to Double
    By vcp in forum Qt Programming
    Replies: 4
    Last Post: 13th June 2008, 17:15
  2. probleme with cast from double to QString
    By hbill in forum Qt Programming
    Replies: 7
    Last Post: 16th May 2008, 23:00
  3. strange problem about QString::number(double)
    By yuzr in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 24th December 2007, 13:05
  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.