在字符串中添加逗号

Add comma's in string

本文关键字:添加 字符串      更新时间:2023-10-16

我想知道如何在int或字符串中设置","逗号?例如,我已经得到了这个:

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

这将显示在UI上。

这个数字有点像123456789,我如何设置字符串为123,456,789的格式?

在QLocale上签出文档http://doc.qt.io/qt-4.8/qlocale.html:

QLocale(QLocale::English).toString(123456789);

您正在研究QLocale::toString(int(

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

注:

  • 不要使用"逗号",在每个地区(语言、国家(这是不同的。看见http://www.gnu.org/software/gettext/manual/gettext.html#Aspects以获取更多示例
  • 如果您使用QString::number((,您将获得一个局部相关的表示

可能:http://www.qtcentre.org/threads/9822-Numbers-with-comma-formatQString number=QLocale(QLocale::English(.toString(123456789,'f',2(;(我还没有测试(

当然!试试这个:

QLocale locale(QLocale::English);
QString string = locale.toString(123456789.21345, 'f');
double n = 123456789.12345;
QString string = QLocale(QLocale::English).toString(n, 'f', 2);

这对我很有效。