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
?
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.
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.
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 .
Checkout the docs on QLocale at http://doc.qt.io/qt-4.8/qlocale.html:
QLocale(QLocale::English).toString(123456789);
You are looking into QLocale::toString(int)
int i = 123456789;
QLocale l = QLocale::system();
QString s = l.toString(i);
Notes:
Yes, for sure! Try this:
QLocale locale(QLocale::English);
QString string = locale.toString(123456789.21345, 'f');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With