Don't localize double numbers in the webui. Closes #1525.

This commit is contained in:
sledgehammer999 2014-08-08 02:46:54 +03:00
parent 4908ed4e78
commit d44df4f985
3 changed files with 27 additions and 20 deletions

View file

@ -259,7 +259,9 @@ int misc::pythonVersion() {
// use Binary prefix standards from IEC 60027-2
// see http://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes
QString misc::friendlyUnit(qreal val, bool is_speed) {
// FIXME: Remove the 'webui' variable, when the webui API is reworked
// to send numbers instead of strings with suffixes
QString misc::friendlyUnit(qreal val, bool is_speed, bool webui) {
if (val < 0)
return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
int i = 0;
@ -269,7 +271,7 @@ QString misc::friendlyUnit(qreal val, bool is_speed) {
if (i == 0)
ret = QString::number((long)val) + " " + QCoreApplication::translate("misc", units[0].source, units[0].comment);
else
ret = accurateDoubleToString(val, 1) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment);
ret = accurateDoubleToString(val, 1, !webui) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment);
if (is_speed)
ret += QCoreApplication::translate("misc", "/s", "per second");
return ret;
@ -585,7 +587,9 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less
}
#endif
QString misc::accurateDoubleToString(const double &n, const int &precision) {
// FIXME: Remove the 'localized' variable, when the webui API is reworked
// to send numbers instead of strings with suffixes
QString misc::accurateDoubleToString(const double &n, const int &precision, bool localized) {
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when
** the number has more digits after the decimal than we want AND the digit after
@ -593,5 +597,8 @@ QString misc::accurateDoubleToString(const double &n, const int &precision) {
** precision we add an extra 0 behind 1 in the below algorithm. */
double prec = std::pow(10.0, precision);
return QLocale::system().toString(std::floor(n*prec)/prec, 'f', precision);
if (localized)
return QLocale::system().toString(std::floor(n*prec)/prec, 'f', precision);
else
return QString::number(std::floor(n*prec)/prec, 'f', precision);
}