Base: add convenience functions to create a QString from quantity or double

This commit is contained in:
wmayer
2021-04-20 14:07:20 +02:00
parent cf23e622ed
commit e4f98b4512
4 changed files with 27 additions and 0 deletions

View File

@@ -63,6 +63,14 @@ QuantityFormat::QuantityFormat()
{
}
QuantityFormat::QuantityFormat(QuantityFormat::NumberFormat format, int decimals)
: option(OmitGroupSeparator | RejectGroupSeparator)
, format(format)
, precision(decimals < 0 ? UnitsApi::getDecimals() : decimals)
, denominator(defaultDenominator)
{
}
// ----------------------------------------------------------------------------
Quantity::Quantity()

View File

@@ -75,6 +75,7 @@ struct BaseExport QuantityFormat {
denominator = denom;
}
QuantityFormat();
QuantityFormat(NumberFormat format, int decimals=-1);
inline char toFormat() const {
switch (format) {
case Fixed:

View File

@@ -154,6 +154,17 @@ void UnitsApi::setSchema(UnitSystem s)
UserPrefSystem->setSchemaUnits(); // if necessary a unit schema can change the constants in Quantity (e.g. mi=1.8km rather then 1.6km).
}
QString UnitsApi::toNumber(const Base::Quantity& q, const QuantityFormat& f)
{
QString number = QString::fromLatin1("%1").arg(q.getValue(), 0, f.toFormat(), f.precision);
return number;
}
QString UnitsApi::toNumber(double d, const QuantityFormat& f)
{
QString number = QString::fromLatin1("%1").arg(d, 0, f.toFormat(), f.precision);
return number;
}
//double UnitsApi::translateUnit(const char* str)
//{

View File

@@ -66,6 +66,13 @@ public:
return UnitsApi::schemaTranslate(quant, dummy1, dummy2);
}
/** Get a number as string for a quantity of a given format.
* The string is a number in C locale (i.e. the decimal separator is always a dot) and if
* needed represented in scientific notation. The string doesn't include the unit of the quantity.
*/
static QString toNumber(const Base::Quantity& q, const QuantityFormat& f = QuantityFormat(QuantityFormat::Default));
static QString toNumber(double d, const QuantityFormat& f = QuantityFormat(QuantityFormat::Default));
/// generate a value for a quantity with default user preferred system
static double toDbl(PyObject *ArgObj,const Base::Unit &u=Base::Unit());
/// generate a value for a quantity with default user preferred system