如何在纯 C++ 或 QT6 中将“½”之类的内容转换为“1/2”?
我看到一个答案,它对特定 unicode 字符串的字典进行了硬编码,但是如何对每个可能的小数部分进行硬编码,而不是对所有内容进行硬编码?
好吧我自己做的哈哈
void convertFractions(QString &input) {
static QRegularExpression fractionRegex(QStringLiteral("[\u00BC-\u00BE\u2150-\u215E]"));
QRegularExpressionMatchIterator i = fractionRegex.globalMatch(input);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString fraction = match.captured(0);
fraction = fraction.normalized(QString::NormalizationForm_KD);
input.replace(match.capturedStart(), match.capturedLength(), fraction);
}
}