Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add comma's in string

Tags:

c++

qt

I was wondering how can I set a ',' comma in an int or string? For example, I already got this:

QString::number(object->number()) 

which will be shown on the UI.

And the number is something like 123456789, how can I set the format that the string will be 123,456,789?

like image 508
user1345821 Avatar asked Jan 16 '13 07:01

user1345821


People also ask

How do you add a comma to a string?

You can use the string function EndsWith to tell if a string ends with a certain string. If it does then you can use Insert to place the comma.

How do you add a comma to a string in Java?

Approach: This can be achieved with the help of join() method of String as follows. Get the Set of String. Form a comma separated String from the Set of String using join() method by passing comma ', ' and the set as parameters. Print the String.

How do you add a comma to a string in Python?

Use str. format(number) with "{:,}" as str to signal the use of a comma for a thousands separator and return a string with commas added to number .


3 Answers

Checkout the docs on QLocale at http://doc.qt.io/qt-4.8/qlocale.html:

QLocale(QLocale::English).toString(123456789);
like image 124
goji Avatar answered Oct 12 '22 21:10

goji


You are looking into QLocale::toString(int)

int i = 123456789;
QLocale l = QLocale::system();
QString s = l.toString(i);

Notes:

  • Do not use "commas", in every locale (language, country) this is different. See http://www.gnu.org/software/gettext/manual/gettext.html#Aspects for more examples.
  • If you use QString::number() you will get a local dependent representation.
like image 37
elcuco Avatar answered Oct 12 '22 22:10

elcuco


Yes, for sure! Try this:

QLocale locale(QLocale::English);
QString string = locale.toString(123456789.21345, 'f');
like image 24
Didac Perez Parera Avatar answered Oct 12 '22 21:10

Didac Perez Parera