Results 1 to 7 of 7

Thread: Displaying an integer with thousands separator

  1. #1
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Displaying an integer with thousands separator

    Hi guys

    The Qt documentation says that both of these methods should give me thousands separators i.e.
    1. using QString("%L1").arg(integerVal)
    2. setting default local to English United states and then using QLocale toString(int);

    What am I doing wrong in the following code or is there some other way of getting thousands separators when displaying an int as a string?

    Thanks
    Jeff

    Qt Code:
    1. int dataAsInt = index.model()->data(index, Qt::DisplayRole).toInt();
    2. //dataDescription = QString("%L1").arg(dataAsInt); // doesn't work
    3.  
    4. QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
    5. QLocale aEnglish;
    6. dataDescription = aEnglish.toString(dataAsInt); // doesn't work
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Displaying an integer with thousands separator

    ?
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. int i = 12657;
    8. qWarning() << QString("%L1").arg(i);
    9. QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
    10. QLocale aEnglish;
    11. qWarning() << aEnglish.toString(i);
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

    gives me
    "12.657"
    "12,657"
    on my mac.

  3. The following user says thank you to Lykurg for this useful post:

    Jeffb (1st February 2012)

  4. #3
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Displaying an integer with thousands separator

    @Lykurg

    Thanks for the speedy reply.
    Yep got it sorted. I'm using a custom delegate and my logic was sending down a path that was using the default data.
    Out of interest - would you use the first or second method assuming the default locale is the same as the system local?

    Jeff

  5. #4
    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: Displaying an integer with thousands separator

    They both work here (Qt 4.7.4):
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. int dataAsInt = 10234;
    9.  
    10. QString dataDescription = QString("%L1").arg(dataAsInt); // doesn't work
    11. qDebug() << dataDescription; // outputs "10,234"
    12.  
    13. QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
    14. QLocale aEnglish;
    15. dataDescription = aEnglish.toString(dataAsInt); // doesn't work
    16. qDebug() << dataDescription; // outputs "10,234"
    17.  
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 
    My default locale is Australia, English with group separator ','.

    So, the question is, "How doesn't it work for you?"


    Edit: D'oh! I should have looked harder at the preview before posting

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Displaying an integer with thousands separator

    Quote Originally Posted by Jeffb View Post
    Out of interest - would you use the first or second method assuming the default locale is the same as the system local?
    Well both work the same. Internally %L1 uses the default local as well. So it is just a matter of taste. Since I am lacy (EDIT: Well if my dictionary is right (spitzenartig in german), I am lacy too, but I wanted to write lazy...) I would go for %L1. Beside for me it looks cleaner.

  7. #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: Displaying an integer with thousands separator

    %L1 makes more sense if the number is only part of your string, e.g.:
    Qt Code:
    1. QString a = QString("I have %L1 apples").arg(1024);
    To copy to clipboard, switch view to plain text mode 
    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.


  8. #7
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Displaying an integer with thousands separator

    Thanks guys.
    I agree that %L1 is easier and looks cleaner.

    Cheers
    Jeff

Similar Threads

  1. Add thousands of item to QListWidget
    By daarsh in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2011, 09:39
  2. Displaying integer values into textbox
    By deck99 in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2011, 13:07
  3. Replies: 1
    Last Post: 30th October 2009, 03:42
  4. Replies: 3
    Last Post: 7th August 2009, 10:21
  5. int to QString with thousands separator
    By klaus1111 in forum Newbie
    Replies: 6
    Last Post: 6th January 2006, 22:06

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.