Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Number formats 00.00

  1. #1
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Number formats 00.00

    Hi guys,

    I need to have 00.00(4 digit number 00 decimal and 2 fraction digists). What can be used from the QString or other classes that can help me to cut or extend these number, so I always have the for digit number, for example:

    1.23 -> 01.23
    0.3 -> 00.30
    12.3 -> 12.30
    0 -> 00.00
    12.3333 -> 12.33
    1.444-> 01.44

    etc.

    Format "%4.2f" does this:

    12.3 -> 12.30
    1.3333->1.33

    but can't handle:
    1.33 -> 01.33
    1.3 -> 01.30

    It add 0 to the fraction , but never add 0 at the beginning of the number.

    Thanks

    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

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

    Default Re: Number formats 00.00

    Perhaps QString::arg() with fill char '0'?
    J-P Nurmi

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

    Default Re: Number formats 00.00

    Try this:
    QString msg = QString("%1").arg(number);
    msg = msg.leftJustified(msg.indexOf('.')+3, '0');
    msg = msg.left(msg.indexOf('.')+3);

    here msg will contains 2 digit after decimal point.
    Last edited by rajesh; 4th October 2007 at 12:54. Reason: code modified

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

    Default Re: Number formats 00.00

    if you want only 4 digit then
    QString msg = QString("%1").arg(number);
    msg = msg.leftJustified(msg.indexOf('.')+3, '0');
    msg = msg.left(msg.indexOf('.')+3);

    msg = msg.rightJustified(5,'0');
    Last edited by rajesh; 4th October 2007 at 12:53. Reason: code added

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

    Default Re: Number formats 00.00

    Quote Originally Posted by rajesh View Post
    QString msg = QString("%1").arg(number);
    msg = msg.leftJustified(msg.indexOf('.')+3, '0');
    msg = msg.left(msg.indexOf('.')+3);

    msg = msg.rightJustified(5,'0');
    Homework: Reduce all of this to a single line of code.

  6. #6
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Number formats 00.00

    Quote Originally Posted by maverick_pol View Post
    Format "%4.2f" does this:

    12.3 -> 12.30
    1.3333->1.33

    but can't handle:
    1.33 -> 01.33
    1.3 -> 01.30

    It add 0 to the fraction , but never add 0 at the beginning of the number.
    I believe if you would have used

    "%04.2f" then you would get 0's to fill in anything to the left to make sure there are 4 digits.

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

    Default Re: Number formats 00.00

    ToddAtWSU,
    do you mean this:

    double time = 1.3333;
    QString str = QString("%04.2f").arg(time );

    in this case str storing 1.3333.2f as I checked in Qt4.3.1

    or
    str = QString("%04.2f").arg(time); also not giving correct result

    so, what is the syntax?
    Last edited by rajesh; 5th October 2007 at 06:23. Reason: added more line

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Number formats 00.00

    I think you missed this version of QString::arg()
    Qt Code:
    1. QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Number formats 00.00

    error C2352: 'QString::arg' : illegal call of non-static member function
    so, I written
    QString::arg(time, 4.2, 'f', 0); but not working

    marcel,
    can you please write a tested syntax?
    Last edited by rajesh; 5th October 2007 at 08:37.

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Number formats 00.00

    arg is not static in QString. you need an instance to use it.
    and there's an example in Assistant.

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

    Default Re: Number formats 00.00

    according to example in Assistant:
    QString("%1").arg(time, 4.2, 'f', 0); is not giving correct output.

  12. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Number formats 00.00

    I don't think it even compiles. The fieldWidth parameter of arg is int not double.

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

    Default Re: Number formats 00.00

    this is tested working solution

    double time = 1.2;
    QString str= QString("%1").arg(time, 0, 'f', 2);
    str= str.rightJustified(5,'0');

    if time = 1.345 str = 01.34
    time = 1.3 str = 01.30

    time = 1234.5678 str = 1234.56

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

    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

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

    Default Re: Number formats 00.00

    ok, final one line solution is:

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

    its working.

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

    Default Re: Number formats 00.00

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

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

    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');

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

    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!

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

    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

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

    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.