Results 1 to 4 of 4

Thread: how to format a number into a const width string ?

  1. #1

    Default how to format a number into a const width string ?

    How to format a number into a const width string ?
    For example, if n is 12345, it is formatted to "12345", if n is 123, it is formated to "00123", if n is 0, it is formatted to "00000".
    thanks!

  2. #2
    Join Date
    Jul 2008
    Posts
    47
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to format a number into a const width string ?

    QString new( QString( "00000%1" ).arg( 1234 ) );
    new = new.right( 5 );

    Others may find more efficient solutions

  3. #3
    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: how to format a number into a const width string ?

    Take a look at 'fieldWidth' and 'fillChar' parameters of QString::arg().
    J-P Nurmi

  4. #4
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to format a number into a const width string ?

    Quote Originally Posted by muellerp View Post
    QString new( QString( "00000%1" ).arg( 1234 ) );
    new = new.right( 5 );

    Others may find more efficient solutions
    that will not compile... "new" is a keyword ;-)

Similar Threads

  1. QShared
    By sabeesh in forum Qt Programming
    Replies: 11
    Last Post: 11th October 2007, 13:40
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  3. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 08:43
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42
  5. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 20:47

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.