Results 1 to 7 of 7

Thread: Convert double to QTrsing with a fixed number of decimals

  1. #1
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Convert double to QTrsing with a fixed number of decimals

    I am converting the time since the start of the program to a QString continuously which I print in the status bar. I print it like this:

    Qt Code:
    1. ui->statusBar->showMessage("Time: " + QString::number(system.get_time(), ) + " s");
    To copy to clipboard, switch view to plain text mode 

    I have already changed the font to a fixed width font ("Courier"), but since the number of decimals after the decimal point keeps changing, the unit ("s") keeps moving forth and back all the time and is barely readable. The maximum number of decimals printed at the same time seems to be 6 though. How can I most easily make sure that 6 decimals are always printed, even if the last of the decimals are zeros?

  2. #2
    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: Convert double to QTrsing with a fixed number of decimals

    Pseudocode:
    while number of decimals < 6: string += '0'
    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.


  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Convert double to QTrsing with a fixed number of decimals

    You can use 'f' instead of default 'g' for format and it should work the way you want:
    Qt Code:
    1. QString s = QString::number(9.56, 'f', 6); //s will be "9.560000"
    To copy to clipboard, switch view to plain text mode 

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

    Yes (30th December 2012)

  5. #4
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Convert double to QTrsing with a fixed number of decimals

    You can use:
    QString::number (1.0, 'f', 6);

    Or better use:
    QString("%L1").arg (1.0, 0, 'f', 6);
    It yields locale-appropriate representation, and also enables you to pad your string on the left side too by specifying fieldWidth:
    QString("%L1").arg (1.0, 12, 'f', 6, '0');

  6. The following 2 users say thank you to lanz for this useful post:

    Yes (30th December 2012), Zlatomir (31st December 2012)

  7. #5
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert double to QTrsing with a fixed number of decimals

    Thanks! I used Zlatomir's method, it was enough for me. Maybe if I would need to fix the number of digits before the decimal point as well I would use lanz' method (because that's what I guess it's for).

  8. #6
    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: Convert double to QTrsing with a fixed number of decimals

    Lanz's locale-specific tweak also ensure the number is displayed with the appropriate decimal point, thousands separator (i.e. '.' or ','), sign character etc. for the users' locale. See QLocale. The field width and fill character can be used without the locale specific stuff.

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

    Yes (30th December 2012)

  10. #7
    Join Date
    Nov 2011
    Posts
    51
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert double to QTrsing with a fixed number of decimals

    Ah, I see.

Similar Threads

  1. Replies: 1
    Last Post: 5th March 2012, 06:34
  2. Convert int to Hex with fixed bytes
    By metRo_ in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2011, 16:31
  3. Print floating and double precision number
    By marc2050 in forum Newbie
    Replies: 2
    Last Post: 17th May 2011, 08:15
  4. Replies: 2
    Last Post: 5th February 2009, 11:21
  5. Cannot convert double to QString
    By maxpower in forum Qt Programming
    Replies: 9
    Last Post: 24th December 2007, 03:04

Tags for this Thread

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.