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

Code before:
        // 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
{
    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
    QRegularExpression rxFormat(QStringLiteral("%[+-]?[0-9]*\\.*[0-9]*[aefgrwAEFGRW]")); //printf double format spec

Review comment (suggestion):
```suggestion
        if (dec.contains(".") {
            while(dec.endsWith("0")) {
                dec.chop(1);
            }
        }
        int dotIndex = dec.indexOf(QStringLiteral("."));
        int nDecimals = dec.size() - dotIndex - 1;
```

Code after:
        // 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
{
    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
    QRegularExpression rxFormat(QStringLiteral("%[+-]?[0-9]*\\.*[0-9]*[aefgrwAEFGRW]")); //printf double format spec
    QRegularExpressionMatch rxMatch;