Improvements on accurateDoubleToString().

This commit is contained in:
sledgehammer999 2014-01-21 17:24:08 +02:00
parent bb7c2dd9c6
commit 3749a30af2
2 changed files with 4 additions and 8 deletions

View file

@ -574,17 +574,13 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less
} }
#endif #endif
QString misc::accurateDoubleToString(double n, int precision) { QString misc::accurateDoubleToString(const double &n, const int &precision) {
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9 /* 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 ** 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 ** the number has more digits after the decimal than we want AND the digit after
** our 'wanted' is >= 5. In this case our last digit gets rounded up. So for each ** our 'wanted' is >= 5. In this case our last digit gets rounded up. So for each
** precision we add an extra 0 behind 1 in the below algorithm. ** precision we add an extra 0 behind 1 in the below algorithm. */
** However this, approach has a drawback. eg (99,99, 2) returns 99.98 and (99.99, 3)
** returns 99.989. This is a minor issue because mostly we want to use precision 1 or 2
** and the double fed into this function will have more decimal places than required
** precision anyway.*/
double prec = std::pow(10.0, precision); double prec = std::pow(10.0, precision);
return QLocale::system().toString((int)(n*(int)prec)/prec, 'f', precision); return QLocale::system().toString(std::floor(n*prec)/prec, 'f', precision);
} }

View file

@ -108,7 +108,7 @@ namespace misc
QList<bool> boolListfromStringList(const QStringList &l); QList<bool> boolListfromStringList(const QStringList &l);
QString toQString(time_t t); QString toQString(time_t t);
QString accurateDoubleToString(double n, int precision); QString accurateDoubleToString(const double &n, const int &precision);
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
bool naturalSort(QString left, QString right, bool& result); bool naturalSort(QString left, QString right, bool& result);