Results 1 to 4 of 4

Thread: How to make a lineEdit show decimal numbers instead of scientific

  1. #1
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default How to make a lineEdit show decimal numbers instead of scientific

    Hi all,

    This is a section of my app which is a calculator in Qt:

    Qt Code:
    1. QTextStream (&ss) << expression(); // expression() return a double value
    2. result_box -> setText(ss); // result_box is a lineEdit
    To copy to clipboard, switch view to plain text mode 


    When I type 10^6 and the function expression() returns that value, in result_box the scientific notation of it will be shown, 1e+06!

    How to do to make the app show the result in decimal, 1000000 rather than that scientific notation please?

  2. #2
    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: How to make a lineEdit show decimal numbers instead of scientific

    Qt Code:
    1. result_box->setText( QString::number(expression(), 'f', 0) );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make a lineEdit show decimal numbers instead of scientific

    You can use like the following::

    result_box->setText( QString::number(expression(), 'd', 0) );

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to make a lineEdit show decimal numbers instead of scientific

    Using 'd' is not explicitely defined. It works only because QString::number() defaults to 'f' if any undefined character is used.
    As you can see from this small example, 'd', 'q', 'a', '#' all produce the same output.
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2.  
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. double dValue = 123e15;
    10.  
    11. qDebug () << 'g' << QString::number (dValue, 'g', 0);
    12. qDebug () << 'f' << QString::number (dValue, 'f', 0);
    13.  
    14. qDebug () << 'd' << QString::number (dValue, 'd', 0);
    15. qDebug () << 'a' << QString::number (dValue, 'a', 0);
    16. qDebug () << 'q' << QString::number (dValue, 'q', 0);
    17. qDebug () << '#' << QString::number (dValue, '#', 0);
    18.  
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    g "1e+17"
    f "123000000000000000"
    d "123000000000000000"
    a "123000000000000000"
    q "123000000000000000"
    # "123000000000000000"

    But please do not use undefined values. They might get meaningful in the future, and you would get unexpected results. As ChrisW67 already wrote using 'f' is the way to go.

    Ginsengelf
    Last edited by Ginsengelf; 14th September 2017 at 09:06. Reason: spelling corrections

Similar Threads

  1. Replies: 3
    Last Post: 15th July 2016, 18:17
  2. How to make LineEdit show all text in uppercase
    By roseicollis in forum Qt Programming
    Replies: 13
    Last Post: 4th March 2015, 15:29
  3. QLineEdit for editing hexadecimal and decimal numbers
    By schall_l in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2012, 11:38
  4. To underline the numbers behind the decimal points?
    By wookoon in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2010, 23:01
  5. Replies: 3
    Last Post: 17th April 2010, 21:35

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.