[TD] Add new non-standard format specifier '%w' that truncates trailing zeros.
This commit is contained in:
@@ -821,7 +821,19 @@ std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int
|
||||
// we reformat the value
|
||||
// the user can overwrite the decimal settings, so we must in every case use the formatSpecifier
|
||||
// the default is: if useDecimals(), then formatSpecifier = global decimals, otherwise it is %.2f
|
||||
formattedValue = QString::asprintf(Base::Tools::toStdString(formatSpecifier).c_str(), userVal);
|
||||
// Also, handle the new non-standard format-specifier '%w', which has the following rules: works as %f, but no trailing zeros
|
||||
if (formatSpecifier.contains(QRegExp(QStringLiteral("%.*[wW]")))) {
|
||||
QString fs = formatSpecifier;
|
||||
fs.replace(QRegExp(QStringLiteral("%(.*)w")), QStringLiteral("%\\1f"));
|
||||
fs.replace(QRegExp(QStringLiteral("%(.*)W")), QStringLiteral("%\\1F"));
|
||||
formattedValue = QString::asprintf(Base::Tools::toStdString(fs).c_str(), userVal);
|
||||
// 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(QRegExp(QStringLiteral("([0-9][0-9]*\\.[0-9]*[1-9])00*$")), QStringLiteral("\\1"));
|
||||
formattedValue.replace(QRegExp(QStringLiteral("([0-9][0-9]*)\\.0*$")), QStringLiteral("\\1"));
|
||||
} else {
|
||||
formattedValue = QString::asprintf(Base::Tools::toStdString(formatSpecifier).c_str(), userVal);
|
||||
}
|
||||
|
||||
// if abs(1 - userVal / formattedValue) > 0.1 we know that we make an error greater than 10%
|
||||
// then we need more digits
|
||||
@@ -969,7 +981,7 @@ QStringList DrawViewDimension::getPrefixSuffixSpec(QString fSpec)
|
||||
QString formatPrefix;
|
||||
QString formatSuffix;
|
||||
//find the %x.y tag in FormatSpec
|
||||
QRegExp rxFormat(QString::fromUtf8("%[+-]?[0-9]*\\.*[0-9]*[aefgAEFG]")); //printf double format spec
|
||||
QRegExp rxFormat(QStringLiteral("%[+-]?[0-9]*\\.*[0-9]*[aefgwAEFGW]")); //printf double format spec
|
||||
QString match;
|
||||
int pos = 0;
|
||||
if ((pos = rxFormat.indexIn(fSpec, 0)) != -1) {
|
||||
|
||||
Reference in New Issue
Block a user