Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-38171

QString::number rounds wrongly

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.7.0 Alpha
    • 5.2.1
    • None
    • Intel Core i7-2600
      Windows 7 Professional x64
      VS2008
    • 8c2ca33073f20e1fe552bd8b5f663aec5b8a634d and follow-ups

    Description

      Here is a short test code that demonstrates the undefined behaviour of QString::number() when rounding floating point numbers. What is the exact type of rounding QString::number() uses?

      #include <QtDebug>
      #include <iostream>
      
      int main(int argc, char *argv[])
      {
      	char szTest[100];
      	qDebug()<<QString::number(115.425, 'f', 2);			// "115.42"
      	sprintf(szTest, "%.2f", 115.425);					
      	qDebug()<<szTest;									// 115.43
      	printf("%.2f\n", 115.425);							// 115.43
      	qDebug()<<QString("%1").arg(115.425, 0, 'f', 2);	// "115.42"
      	qDebug()<<QString::number(15.425, 'f', 2);			// "15.43" - this one is rounded correctly
      	qDebug()<<QString::number(115.4250000001, 'f', 2);	// "115.43"
      
      	qDebug()<<QString::number(78.625, 'f', 2);			// "78.62"
      	sprintf(szTest, "%.2f", 78.625);					
      	qDebug()<<szTest;									// 78.63
      	printf("%.2f\n", 78.625);							// 78.63
      	qDebug()<<QString("%1").arg(78.625, 0, 'f', 2);		// "78.62"
      	qDebug()<<QString::number(8.625, 'f', 2);			// "8.62"
      	qDebug()<<QString::number(78.6250000001, 'f', 2);	// "78.63"
      
      	std::cin>>szTest;
      
      	return 0;
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ulherman Ulf Hermann
            emobemo Emil Ivanov
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes