Base: introduce 'getSafeUserUnit' that fallbacks to full precision and internal unit ...

... in case unit schema would zero a non-zero quantity

 For example, using '1 mil' with UnitsSchemaImperialDecimal schema
This commit is contained in:
0penBrain
2022-07-30 14:58:31 +02:00
parent 65f8321e27
commit 0d577a8789
4 changed files with 188 additions and 171 deletions

View File

@@ -242,6 +242,22 @@ QString Quantity::getUserString(UnitsSchema* schema, double &factor, QString &un
return schema->schemaTranslate(*this, factor, unitString);
}
QString Quantity::getSafeUserString() const
{
auto retString = getUserString();
if(Q_LIKELY(this->_Value != 0))
{
auto feedbackQty = parse(retString);
auto feedbackVal = feedbackQty.getValue();
if (feedbackVal == 0) {
retString = QStringLiteral("%1 %2")
.arg(this->_Value)
.arg(this->getUnit().getString());
}
}
return retString;
}
/// true if it has a number without a unit
bool Quantity::isDimensionless() const
{