Results 1 to 20 of 22

Thread: Number formats 00.00

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Number formats 00.00

    The correct version of QString::arg() was already stated in the second post of this thread.

    A hint:
    • fieldWidth = 5 (4 digits + decimal point)
    • format = 'f'
    • precision = 2 (number of digits after the decimal point)
    • fillChar = '0'
    J-P Nurmi

  2. #2
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    7
    Thanked 25 Times in 24 Posts

    Default Re: Number formats 00.00

    ok, final one line solution is:

    QString str= QString("%1").arg(data, 5, 'f',2, '0');

    its working.

  3. #3
    Join Date
    Jan 2007
    Posts
    177
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 10 Times in 9 Posts

    Default Re: Number formats 00.00

    whats with QString().sprintf("%.2f");

  4. #4
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    7
    Thanked 25 Times in 24 Posts

    Default Re: Number formats 00.00

    jacel,
    Homework: Reduce all of this to a single line of code.
    finally homework done.
    QString str= QString("%1").arg(data, 5, 'f',2, '0');

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Number formats 00.00

    Quote Originally Posted by rajesh View Post
    finally homework done.
    QString str= QString("%1").arg(data, 5, 'f',2, '0');
    Good, the other solution, as kernel_panic has suggested, is QString().sprintf( "%05.2f", data ).

    Remember, be lazy!

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Number formats 00.00

    "Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or QString::arg(), both of which support Unicode strings seamlessly and are type-safe."
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Number formats 00.00

    Quote Originally Posted by jpn View Post
    "Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or QString::arg(), both of which support Unicode strings seamlessly and are type-safe."
    Indeed. Read the docs, kids, even if you know them by heart.

  8. #8
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    7
    Thanked 25 Times in 24 Posts

    Default Re: Number formats 00.00

    I want to know which solution is accepted by maverick_pol...

Similar Threads

  1. Line Number - QTextEdit...???
    By deepusrp in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 16:34
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

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.