From b0d29b745cb8018a6fd103ee229924b3880d675a Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 20 Apr 2021 14:56:29 +0200 Subject: [PATCH] Base: add convenience functions to create a QString from quantity --- src/Base/UnitsApi.cpp | 12 +++++++++--- src/Base/UnitsApi.h | 9 +++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Base/UnitsApi.cpp b/src/Base/UnitsApi.cpp index 5512a438f9..b42d7f29bd 100644 --- a/src/Base/UnitsApi.cpp +++ b/src/Base/UnitsApi.cpp @@ -154,15 +154,21 @@ 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::toString(const Base::Quantity& q, const QuantityFormat& f) +{ + QString value = QString::fromLatin1("'%1 %2'").arg(q.getValue(), 0, f.toFormat(), f.precision+1) + .arg(q.getUnit().getString()); + return value; +} + 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; + return toNumber(q.getValue(), f); } QString UnitsApi::toNumber(double d, const QuantityFormat& f) { - QString number = QString::fromLatin1("%1").arg(d, 0, f.toFormat(), f.precision); + QString number = QString::fromLatin1("%1").arg(d, 0, f.toFormat(), f.precision+1); return number; } diff --git a/src/Base/UnitsApi.h b/src/Base/UnitsApi.h index 17d4d36ab5..594878d7e7 100644 --- a/src/Base/UnitsApi.h +++ b/src/Base/UnitsApi.h @@ -66,11 +66,20 @@ 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 also includes the unit of the quantity. + */ + static QString toString(const Base::Quantity& q, const QuantityFormat& f = QuantityFormat(QuantityFormat::Default)); /** 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)); + /** Get a number as string for a double 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(double d, const QuantityFormat& f = QuantityFormat(QuantityFormat::Default)); /// generate a value for a quantity with default user preferred system