Ref T424, PQ strings without (thousand) separators

* separator can cause issue when sending data (WebService, FSD)
* re-parsing (print and parse again) can cause to undesired results as the separator is mistaken as decimal separator with a different locale
* separator now a parameter so it can still be used
This commit is contained in:
Klaus Basan
2018-11-04 00:33:12 +01:00
parent 7b209685dd
commit ef21ec67f3
6 changed files with 36 additions and 27 deletions

View File

@@ -35,9 +35,9 @@ namespace BlackMisc
return m_data->m_fromDefault(unit.m_data->m_toDefault(value)); return m_data->m_fromDefault(unit.m_data->m_toDefault(value));
} }
QString CMeasurementUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const QString CMeasurementUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
{ {
return this->makeRoundedQString(value, digits).append(this->getSymbol(i18n)); return this->makeRoundedQString(value, digits, withGroupSeparator).append(this->getSymbol(i18n));
} }
double CMeasurementUnit::roundValue(double value, int digits) const double CMeasurementUnit::roundValue(double value, int digits) const
@@ -52,12 +52,21 @@ namespace BlackMisc
return CMathUtils::roundEpsilon(value, this->getEpsilon()); return CMathUtils::roundEpsilon(value, this->getEpsilon());
} }
QString CMeasurementUnit::makeRoundedQString(double value, int digits, bool i18n) const QString CMeasurementUnit::makeRoundedQString(double value, int digits, bool withGroupSeparator, bool i18n) const
{ {
Q_UNUSED(i18n); Q_UNUSED(i18n);
if (digits < 0) digits = m_data->m_displayDigits; if (digits < 0) { digits = m_data->m_displayDigits; }
const double v = CMathUtils::round(value, digits); const double v = CMathUtils::round(value, digits);
const QString s = QLocale::system().toString(v, 'f', digits);
// create locale without separator
static const QLocale localeWithoutSeparator = []
{
QLocale q = QLocale::system();
q.setNumberOptions(QLocale::OmitGroupSeparator);
return q;
}();
const QString s = (withGroupSeparator ? QLocale::system() : localeWithoutSeparator).toString(v, 'f', digits);
return s; return s;
} }
} // namespace } // namespace

View File

@@ -314,11 +314,11 @@ namespace BlackMisc
//! Rounded string utility method, virtual so units can have specialized formatting //! Rounded string utility method, virtual so units can have specialized formatting
//! \note default digits is CMeasurementUnit::getDisplayDigits //! \note default digits is CMeasurementUnit::getDisplayDigits
virtual QString makeRoundedQString(double value, int digits = -1, bool i18n = false) const; virtual QString makeRoundedQString(double value, int digits = -1, bool withGroupSeparator = false, bool i18n = false) const;
//! Value rounded with unit, e.g. "5.00m", "30kHz" //! Value rounded with unit, e.g. "5.00m", "30kHz"
//! \note default digits is CMeasurementUnit::getDisplayDigits //! \note default digits is CMeasurementUnit::getDisplayDigits
virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const; virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool withGroupSeparator = false, bool i18n = false) const;
//! Threshold for comparions //! Threshold for comparions
double getEpsilon() const double getEpsilon() const

View File

@@ -42,7 +42,7 @@ namespace BlackMisc
namespace PhysicalQuantities namespace PhysicalQuantities
{ {
template <class MU, class PQ> template <class MU, class PQ>
const MU & CPhysicalQuantity<MU, PQ>::getUnit() const const MU &CPhysicalQuantity<MU, PQ>::getUnit() const
{ {
return m_unit; return m_unit;
} }
@@ -314,18 +314,18 @@ namespace BlackMisc
} }
template <class MU, class PQ> template <class MU, class PQ>
QString CPhysicalQuantity<MU, PQ>::valueRoundedWithUnit(const MU &unit, int digits, bool i18n) const QString CPhysicalQuantity<MU, PQ>::valueRoundedWithUnit(const MU &unit, int digits, bool withGroupSeparator, bool i18n) const
{ {
Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO, "Cannot convert to null"); Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO, "Cannot convert to null");
if (this->isNull()) { return this->convertToQString(i18n); } if (this->isNull()) { return this->convertToQString(i18n); }
return unit.makeRoundedQStringWithUnit(this->value(unit), digits, i18n); return unit.makeRoundedQStringWithUnit(this->value(unit), digits, withGroupSeparator, i18n);
} }
template <class MU, class PQ> template <class MU, class PQ>
QString CPhysicalQuantity<MU, PQ>::valueRoundedWithUnit(int digits, bool i18n) const QString CPhysicalQuantity<MU, PQ>::valueRoundedWithUnit(int digits, bool withGroupSeparator, bool i18n) const
{ {
if (this->isNull()) { return this->convertToQString(i18n); } if (this->isNull()) { return this->convertToQString(i18n); }
return this->valueRoundedWithUnit(m_unit, digits, i18n); return this->valueRoundedWithUnit(m_unit, digits, withGroupSeparator, i18n);
} }
template<class MU, class PQ> template<class MU, class PQ>

View File

@@ -47,11 +47,11 @@ namespace BlackMisc
*/ */
template <class MU, class PQ> class CPhysicalQuantity : template <class MU, class PQ> class CPhysicalQuantity :
public Mixin::DBusOperators<CPhysicalQuantity<MU, PQ>>, public Mixin::DBusOperators<CPhysicalQuantity<MU, PQ>>,
public Mixin::JsonOperators<CPhysicalQuantity<MU, PQ>>, public Mixin::JsonOperators<CPhysicalQuantity<MU, PQ>>,
public Mixin::Index<PQ>, public Mixin::Index<PQ>,
public Mixin::MetaType<PQ>, public Mixin::MetaType<PQ>,
public Mixin::String<PQ>, public Mixin::String<PQ>,
public Mixin::Icon<CPhysicalQuantity<MU, PQ>> public Mixin::Icon<CPhysicalQuantity<MU, PQ>>
{ {
//! \copydoc CValueObject::compare //! \copydoc CValueObject::compare
friend int compare(const PQ &a, const PQ &b) { return compareImpl(a, b); } friend int compare(const PQ &a, const PQ &b) { return compareImpl(a, b); }
@@ -122,11 +122,11 @@ namespace BlackMisc
//! Value to QString with the given unit, e.g. "5.00m" //! Value to QString with the given unit, e.g. "5.00m"
//! \note default digits is CMeasurementUnit::getDisplayDigits //! \note default digits is CMeasurementUnit::getDisplayDigits
QString valueRoundedWithUnit(const MU &unit, int digits = -1, bool i18n = false) const; QString valueRoundedWithUnit(const MU &unit, int digits = -1, bool withGroupSeparator = false, bool i18n = false) const;
//! Value to QString with the current unit, e.g. "5.00m" //! Value to QString with the current unit, e.g. "5.00m"
//! \note default digits is CMeasurementUnit::getDisplayDigits //! \note default digits is CMeasurementUnit::getDisplayDigits
QString valueRoundedWithUnit(int digits = -1, bool i18n = false) const; QString valueRoundedWithUnit(int digits = -1, bool withGroupSeparator = false, bool i18n = false) const;
//! Round current value in current unit to epsilon //! Round current value in current unit to epsilon
//! \sa CMeasurementUnit::roundToEpsilon //! \sa CMeasurementUnit::roundToEpsilon

View File

@@ -41,9 +41,9 @@ namespace BlackMisc
void CAccelerationUnit::anchor() void CAccelerationUnit::anchor()
{ } { }
QString CAngleUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const QString CAngleUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
{ {
if (digits < 0) digits = this->getDisplayDigits(); if (digits < 0) { digits = this->getDisplayDigits(); }
QString s; QString s;
if ((*this) == CAngleUnit::sexagesimalDeg()) if ((*this) == CAngleUnit::sexagesimalDeg())
{ {
@@ -68,14 +68,14 @@ namespace BlackMisc
} }
else else
{ {
s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, i18n); s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, withGroupSeparator, i18n);
} }
return s; return s;
} }
QString CTimeUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const QString CTimeUnit::makeRoundedQStringWithUnit(double value, int digits, bool withGroupSeparator, bool i18n) const
{ {
if (digits < 0) digits = this->getDisplayDigits(); if (digits < 0) { digits = this->getDisplayDigits(); }
QString s; QString s;
if ((*this) == CTimeUnit::hms()) if ((*this) == CTimeUnit::hms())
{ {
@@ -110,7 +110,7 @@ namespace BlackMisc
} }
else else
{ {
s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, i18n); s = this->CMeasurementUnit::makeRoundedQStringWithUnit(value, digits, withGroupSeparator, i18n);
} }
return s; return s;
} }

View File

@@ -200,7 +200,7 @@ namespace BlackMisc
} }
//! \copydoc CMeasurementUnit::makeRoundedQStringWithUnit //! \copydoc CMeasurementUnit::makeRoundedQStringWithUnit
virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const override; virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool withGroupSeparator = false, bool i18n = false) const override;
//! Radians //! Radians
static const CAngleUnit &rad() static const CAngleUnit &rad()
@@ -820,7 +820,7 @@ namespace BlackMisc
} }
//! \copydoc CMeasurementUnit::makeRoundedQStringWithUnit //! \copydoc CMeasurementUnit::makeRoundedQStringWithUnit
virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool i18n = false) const override; virtual QString makeRoundedQStringWithUnit(double value, int digits = -1, bool withGroupSeparator = false, bool i18n = false) const override;
//! Second s //! Second s
static const CTimeUnit &s() static const CTimeUnit &s()