Centralize dropping of trailing zeroes when converting doubles

We're never interested in trailing zeroes, unless the number is exactly
0. qdtoa would return an empty string if the result was exactly '0',
which is also fixed by this change.

Change-Id: I3ba2f7e835b92d54d9008ad03fdf6ce5fb3af8a4
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
bb10
Ulf Hermann 2015-11-09 11:48:39 +01:00
parent 5c8c4d0b21
commit 4c2dd1a9c1
2 changed files with 3 additions and 10 deletions

View File

@ -2757,12 +2757,7 @@ QString QLocaleData::doubleToString(const QChar _zero, const QChar plus, const Q
if (qstrncmp(buf.data(), "inf", 3) == 0 || qstrncmp(buf.data(), "nan", 3) == 0) {
num_str = QString::fromLatin1(buf.data(), length);
} else { // Handle normal numbers
// Chop trailing zeros
int last_nonzero_idx = length - 1;
while (last_nonzero_idx > 0 && buf[last_nonzero_idx] == '0')
--last_nonzero_idx;
QString digits = QString::fromLatin1(buf.data(), last_nonzero_idx + 1);
QString digits = QString::fromLatin1(buf.data(), length);
if (_zero.unicode() != '0') {
ushort z = _zero.unicode() - '0';

View File

@ -267,6 +267,8 @@ void doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char *
}
}
#endif // QT_NO_DOUBLECONVERSION || QT_BOOTSTRAPPED
while (length > 1 && buf[length - 1] == '0') // drop trailing zeroes
--length;
}
double asciiToDouble(const char *num, int numLen, bool &ok, int &processed)
@ -546,10 +548,6 @@ QString qdtoa(qreal d, int *decpt, int *sign)
doubleToAscii(d, QLocaleData::DFSignificantDigits, QLocale::FloatingPointShortest, result,
QLocaleData::DoubleMaxSignificant + 1, nonNullSign, length, nonNullDecpt);
// Skip trailing zeroes. The DoubleMaxSignificant precision is the worst case.
while (length > 0 && result[length - 1] == '0')
--length;
if (sign)
*sign = nonNullSign ? 1 : 0;
if (decpt)