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

Code before:

    //tolerance not specified, so just format dimension value?
    std::string formattedValue = formatValue(m_dimension->getDimValue(), qFormatSpec, partial, true);

    return formattedValue;
}

// format the value using the formatSpec. Also, handle the non-standard format-
// specifiers '%w', which works as %f but without trailing zeros
// and '%r', which round the value to the given precision.
QString DimensionFormatter::formatValueToSpec(const double value, const QString& formatSpecifier) const
{
    QString formattedValue;
    if (formatSpecifier.contains(QRegularExpression(QStringLiteral("%.*[wW]")))) {
        QString fs = formatSpecifier;
        fs.replace(QRegularExpression(QStringLiteral("%(.*)w")), QStringLiteral("%\\1f"));
        fs.replace(QRegularExpression(QStringLiteral("%(.*)W")), QStringLiteral("%\\1F"));
        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;
}

Review comment (suggestion):
Optional out of scope of this PR. Instead of copying the string (multiple places in the function), it should just be copied as a function parameter `QString&` -> `QString`

Code after:

    //tolerance not specified, so just format dimension value?
    std::string formattedValue = formatValue(m_dimension->getDimValue(), qFormatSpec, partial, true);

    return formattedValue;
}

// format the value using the formatSpec. Also, handle the non-standard format-
// specifiers '%w', which works as %f but without trailing zeros
// and '%r', which round the value to the given precision.
QString DimensionFormatter::formatValueToSpec(const double value, QString formatSpecifier) const
{
    QString formattedValue;

    constexpr auto format = [](QString f, double value){
        return QString::asprintf(f.toStdString().c_str(), value);
    };

    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;