在一个字符串中,每三个数字添加一个,

QString addMillage(QString str){
QLocale locale(QLocale::English, QLocale::UnitedStates);
QRegExp rx("\b\d+(\.\d+)?");
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
QString numStr = rx.capturedTexts()[0];
double num = numStr.toDouble();
int dotCnt = 0;
if(!numStr.contains("."))
dotCnt = 0;
else
dotCnt = numStr.length() - numStr.indexOf(".") -1;
QString formattedStr = locale.toString(num, 'f',dotCnt);
str.replace(pos, rx.matchedLength(), formattedStr);
pos += formattedStr.length();
}
return str;
}

posted @ 2022-12-29 17:51  南荣  阅读(151)  评论(0)    收藏  举报