Results 1 to 4 of 4

Thread: How to display a double?

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default How to display a double?

    Hello, I would like to display a double number into a MessageBox:

    if I write:
    Qt Code:
    1. double d = 0.75;
    2. QString str = QString("delta: %1").arg(d, 0, 'g', 6);
    3. QMessageBox::information(0,"Time","Value: "+str+".\n");
    To copy to clipboard, switch view to plain text mode 
    i's display ok: Value: 0.75.

    But if I write:
    Qt Code:
    1. double d = 3/4;
    2. QString str = QString("delta: %1").arg(d, 0, 'g', 6);
    3. QMessageBox::information(0,"Time","Value: "+str+".\n");
    To copy to clipboard, switch view to plain text mode 
    i's display wrong: Value: 0.. Why and what I should write to display Value: 0.75?

  2. #2
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display a double?

    Hi,

    You can use this code:

    QString("%L0").arg(v,0,'f',2)

    ***Note the use of 'f' parameter.***

    for example:

    double v = 150.63;

    QString qsValue = QString("%L0").arg(v,0,'f',2);

    qDebug() << qsValue;

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to display a double?

    Quote Originally Posted by TomASS View Post
    But if I write:
    Qt Code:
    1. double d = 3/4;
    2. QString str = QString("delta: %1").arg(d, 0, 'g', 6);
    3. QMessageBox::information(0,"Time","Value: "+str+".\n");
    To copy to clipboard, switch view to plain text mode 
    i's display wrong: Value: 0.. Why and what I should write to display Value: 0.75?
    3/4 is 0. If you divide two integers the result is also an integer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    May 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to display a double?

    Try it:

    Qt Code:
    1. double d = 3.0 / 4;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Double Click Capturing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2011, 14:12
  2. OS/X how to get physical display handle
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2009, 19:51
  3. updating a widget's display
    By jhearon in forum Qt Programming
    Replies: 5
    Last Post: 10th November 2008, 16:59
  4. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  5. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08

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.