首页 > 其他分享 >在一个字符串中,每三个数字添加一个,

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

时间:2022-12-29 18:00:41浏览次数:37  
标签:numStr dotCnt 数字 rx pos 添加 QString str 字符串

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;
}

标签:numStr,dotCnt,数字,rx,pos,添加,QString,str,字符串
From: https://www.cnblogs.com/nanrong/p/17013160.html

相关文章