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

Code before:

    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.back().toLatin1() == '0')
        {
            formattedValue.chop(1);
        }
        // Second, try to cut also decimal dot
        if(formattedValue.back().toLatin1() == '.')
        {
            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.back().toLatin1() == '0')
            {
                dec.chop(1);
            }
            dotIndex = dec.indexOf(QStringLiteral("."));
            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

Review comment (suggestion):
```suggestion
        while(formattedValue.endsWith("0")) {
            formattedValue.chop(1);
        }
        // Second, try to cut also decimal dot
        if(formattedValue.endsWith("."))  {
            formattedValue.chop(1);
        }
```

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
{