Repository: FreeCAD/FreeCAD
File: src/Mod/TechDraw/App/DimensionFormatter.cpp (C++)

Code before:
        formattedValue = QString::asprintf(fs.toStdString().c_str(), value);
        // First, try to cut trailing zeros, if AFTER decimal dot there are nonzero numbers
        // Second, try to cut also decimal dot and zeros, if there are just zeros after it
        formattedValue.replace(QRegularExpression(QStringLiteral("([0-9][0-9]*\\.[0-9]*[1-9])00*$")), QStringLiteral("\\1"));
        formattedValue.replace(QRegularExpression(QStringLiteral("([0-9][0-9]*)\\.0*$")), QStringLiteral("\\1"));
    }
    else if (formatSpecifier.contains(QRegularExpression(QStringLiteral("%.*[rR]")))) {
        // round the value to the given precision
        QString fs = formatSpecifier;
        QRegularExpression re(QStringLiteral("%(.*)[rR]"));
        QRegularExpressionMatch match = re.match(fs);
        double roundValue = value;
        if (match.hasMatch()) {
            QString rounderStr = match.captured(1);
            double rounder = rounderStr.toDouble();
            // count rounder decimals
            int dotIndex = rounderStr.indexOf(QStringLiteral("."));
            if (dotIndex < 0){
                fs = QStringLiteral("%.0f");
            } else {
                int roundDec = rounderStr.size() - dotIndex - 1;
                fs = QStringLiteral("%.") + QString::number(roundDec) + QStringLiteral("f");
            }
            roundValue = std::ceil(value / rounder) * rounder;
            formattedValue = QString::asprintf(fs.toStdString().c_str(), roundValue);
        }
    } else {
        if (isNumericFormat(formatSpecifier)) {
            formattedValue = QString::asprintf(formatSpecifier.toStdString().c_str(), value);
        }
    }

    return formattedValue;
}

bool DimensionFormatter::isNumericFormat(const QString& formatSpecifier) const
{
    QRegularExpression rxFormat(QStringLiteral("%[+-]?[0-9]*\\.*[0-9]*[aefgwAEFGW]")); //printf double format spec
    QRegularExpressionMatch rxMatch;
    int pos = formatSpecifier.indexOf(rxFormat, 0, &rxMatch);
    if (pos != -1)  {
        return true;
    }
    return false;
}

//TODO: similar code here and above
QStringList DimensionFormatter::getPrefixSuffixSpec(const QString& fSpec) const
{
    QStringList result;
    //find the %x.y tag in FormatSpec

Review comment (suggestion):
If no match, we need to return something.

```suggestion
        double roundValue = value;
        if (match.hasMatch()) {
            QString rounderStr = match.captured(1);
            double rounder = rounderStr.toDouble();
            // count rounder decimals
            int dotIndex = rounderStr.indexOf(QStringLiteral("."));
            if (dotIndex < 0){
                fs = QStringLiteral("%.0f");
            } else {
                int roundDec = rounderStr.size() - dotIndex - 1;
                fs = QStringLiteral("%.") + QString::number(roundDec) + QStringLiteral("f");
            }
            roundValue = std::ceil(value / rounder) * rounder;
        }
        formattedValue = QString::asprintf(fs.toStdString().c_str(), roundValue);
```

Code after:

    QRegularExpression wrRegExp(QStringLiteral("%(?<dec>.*)(?<spec>[wWrR])"));
    QRegularExpressionMatch wrMatch = wrRegExp.match(formatSpecifier);

    if (! wrMatch.hasMatch()) {
        if (isNumericFormat(formatSpecifier)) {
            formattedValue = format(formatSpecifier, value);
        }
        return formattedValue;
    }

    QString spec = wrMatch.captured(QStringLiteral("spec")).toLower();
    QString dec = wrMatch.captured(QStringLiteral("dec"));

    if (spec == QStringLiteral("w")) {
        formattedValue = format(QStringLiteral("%") + dec + QStringLiteral("f"), value);
        // First, cut trailing zeros
        while(formattedValue.endsWith(QStringLiteral("0")))
        {
            formattedValue.chop(1);
        }
        // Second, try to cut also decimal dot
        if(formattedValue.endsWith(QStringLiteral(".")))
        {
            formattedValue.chop(1);
        }
    }
    else if (spec == QStringLiteral("r")) {
        // round the value to the given precision
        double rounder = dec.toDouble();
        double roundValue = std::ceil(value / rounder) * rounder;
        // format the result with the same decimal count than the rounder
        int dotIndex = dec.indexOf(QStringLiteral("."));
        int nDecimals = 0;
        if (dotIndex >= 0){
            // remove trailing zeros to avoid decimal overwriting
            while(dec.endsWith(QStringLiteral("0")))
            {
                dec.chop(1);
            }
            nDecimals = dec.size() - dotIndex - 1;
        }
        formatSpecifier = QStringLiteral("%.") + QString::number(nDecimals) + QStringLiteral("f");
        formattedValue = format(formatSpecifier, roundValue);
    }

    return formattedValue;
}

bool DimensionFormatter::isNumericFormat(const QString& formatSpecifier) const
{