Results 1 to 7 of 7

Thread: int to QString with thousands separator

  1. #1
    Join Date
    Jan 2006
    Location
    Stuttgart
    Posts
    10
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default int to QString with thousands separator

    Hi,
    is there a Qt-way to convert an integer (for example a file size) into a string with dots between the digits?

    like this: 1.234.567

    Klaus

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: int to QString with thousands separator

    you can use QString::number() to convert the int into a QString and then according to the length of the string you can insert '.' where ever you want.

  3. #3
    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: int to QString with thousands separator

    Quote Originally Posted by klaus1111
    Hi,
    is there a Qt-way to convert an integer (for example a file size) into a string with dots between the digits?

    like this: 1.234.567
    If you use QLocale::toString with properly set locale, you'll get your dotted output

  4. #4
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: int to QString with thousands separator

    Of course you can use this code...
    Qt Code:
    1. QString word = QString::number(num, 10);
    2. int w_size = word.size();
    3. if (w_size > 3) {
    4. word.insert(word.size() - 3, ".");
    5. }
    6. if (w_size > 6) {
    7. word.insert(word.size() - 7, ".");
    8. }
    To copy to clipboard, switch view to plain text mode 
    Regards,
    Luka

  5. #5
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: int to QString with thousands separator

    Quote Originally Posted by wysota
    If you use QLocale::toString with properly set locale, you'll get your dotted output
    Or any other localized output in the world. I've used it to display prices and it is a true timesaver. I think it's available only after Qt 3.3.2.

  6. #6
    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: int to QString with thousands separator

    Eeem... 3.3.0 x.y.z versions are just bug fixes, no new features are added, so it must have been a "0" release. But the point here is that with using QLocale you can have output from a different locale than the rest of the application uses.

  7. #7
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: int to QString with thousands separator

    Quote Originally Posted by wysota
    Eeem... 3.3.0 x.y.z versions are just bug fixes, no new features are added, so it must have been a "0" release. But the point here is that with using QLocale you can have output from a different locale than the rest of the application uses.
    Yes fast typing played it's role . Just confirmed no QLocale in 3.2.x, it's a 3.3.x addition
    Last edited by yop; 6th January 2006 at 23:08.

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 17:26
  2. easiest Way QString can do
    By baray98 in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2008, 21:49
  3. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 21:47
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 21: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.