C
C
CPPJunior2015-06-01 11:58:28
Qt
CPPJunior, 2015-06-01 11:58:28

Why is the number not being converted to a string correctly?

Hello.
I recently discovered some weird QString-related crap, namely the conversion of double to QString:

std::ofstream debug("debug.txt");

debug << "QString::number(5120.0) = " << QString::number(5120.0).toStdString() << std::endl;
debug << "QString::number(5121.0) = " << QString::number(5121.0).toStdString() << std::endl;
debug << "QString::number(5122.0) = " << QString::number(5122.0).toStdString() << std::endl;

debug << "QString(\"%1\").arg(5120.0) = " << QString("%1").arg(5120.0).toStdString() << std::endl;
debug << "QString(\"%1\").arg(5121.0) = " << QString("%1").arg(5121.0).toStdString() << std::endl;
debug << "QString(\"%1\").arg(5122.0) = " << QString("%1").arg(5122.0).toStdString() << std::endl;

debug.close();

Result:
QString::number(5120.0) = 5120
QString::number(5121.0) = 5120.:
QString::number(5122.0) = 5122
QString("%1").arg(5120.0) = 5120
QString("% 1").arg(5121.0) = 5120.:
QString("%1").arg(5122.0) =
5122 the number 5121.0 is converted to "5120.:" for some unknown reason. With this in mind, I have a couple of questions:
  1. Has anyone met with something like this?
  2. Can anyone check the code to see if the result is the same?
  3. What can be wrong?

Thanks in advance!
PS: Qt 4.8.6 + Qt Creator + Visual C++ 2008.
Update:
Tried qt 4.8.7 same story. On qt 5.4.1 + msvc2013, by the way, everything is ok.
In addition, I created a test project in which I checked the conversion and everything turned out to be fine, i.e. the number is normally converted to a string as expected. As far as I understand, all methods eventually use QLocale::toString, so most likely I have something with the locale

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question