Skip to content

Commit

Permalink
V2 services: improve formatting of float values
Browse files Browse the repository at this point in the history
Format float values with max 5 decimals and trim excess zeroes from the
end. This is to avoid long strings of random digits at the end of
values, especially the load averages from GetBackendStatus.
  • Loading branch information
bennettpeter committed Oct 24, 2021
1 parent 5415d49 commit 3b99d0a
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion mythtv/libs/libmythbase/http/serialisers/mythxmlserialiser.cpp
@@ -1,5 +1,6 @@
// Qt
#include <QMetaProperty>
#include <QRegularExpression>

// MythTV
#include "mythdate.h"
Expand Down Expand Up @@ -45,7 +46,12 @@ void MythXMLSerialiser::AddValue(const QString& Name, const QVariant& Value)
return;
}

switch (Value.type())
// The QT documentation states that Value.type() returns
// a QVariant::Type which should be treated as a QMetaType::Type.
// There is no QVariant::Float only a QMetaType::Float so we
// need to cast it here so we can use QMetaType::Float without
// warning messages.
switch (static_cast<QMetaType::Type>(Value.type()))
{
case QVariant::StringList: AddStringList(Value); break;
case QVariant::List: AddList(Name, Value); break;
Expand All @@ -59,6 +65,18 @@ void MythXMLSerialiser::AddValue(const QString& Name, const QVariant& Value)
m_writer.writeCharacters(MythDate::toString(dt, MythDate::ISODate));
}
break;
case QMetaType::Float:
{
// Set max 5 decimals and remove excess zeroes
// This is to avoid very long decimal results with a lot of
// garbage extra numbers at the back, especially in the Load
// Average values in GetBackendStatus.
QString tempstr;
tempstr = QString::number(Value.toFloat(),'f',5);
tempstr.replace(QRegularExpression("0+$"),"0");
m_writer.writeCharacters(tempstr);
break;
}
default:
m_writer.writeCharacters(Value.toString());
}
Expand Down

0 comments on commit 3b99d0a

Please sign in to comment.