Results 1 to 3 of 3

Thread: double to QString

  1. #1
    Join Date
    Oct 2007
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default double to QString

    729194496 B = 695.4 MB
    2x695.4 MB = 145838898 B = 1.35 GB
    1 GB = 2^30 B = 1073741824 B

    and now
    Qt Code:
    1. QFileInfo file("movie.avi");
    2. qint64 size = file.size(); // 729194496 B
    3. size += size; // 145838898 B
    4. double z = size/1073741824;
    5. QString str;
    6. str = QString::number( z, 'f', 2 ); // 1.00 ( WRONG )
    7. str.sprint("%.2f", z ); //1.00
    8.  
    9. int x = floor( z ); // x = 1, qRound( z ) = 1 ( GOOD )
    10. int y = floor( (z - x )*100 ); // y=0 ( WRONG )
    11. str = QString::number(x)+"."+QString::number(y); //1.00 ( WRONG )
    To copy to clipboard, switch view to plain text mode 

    So, how can i get 1.35 GB ?

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

    Default Re: double to QString

    How about:
    Qt Code:
    1. qint64 GB = 1024*1024*1024;
    2. QString str = QString::number(((double)file.size())/((double)(GB)));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: double to QString

    Qt Code:
    1. double z = size/1073741824;
    To copy to clipboard, switch view to plain text mode 
    size is an integer. Result of a division of two integers is another integer. You should do
    Qt Code:
    1. double z = ((double)size) / 1073741824;
    To copy to clipboard, switch view to plain text mode 

    Better is the way suggested by wysota.

Similar Threads

  1. Cannot convert double to QString
    By maxpower in forum Qt Programming
    Replies: 9
    Last Post: 24th December 2007, 03:04
  2. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  4. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

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
  •  
Qt is a trademark of The Qt Company.