简体   繁体   中英

bug in g++ std::strtod function when used in Qt project?

I use 64-bit ubuntu 18.04 upgraded from 16.04.

Make QWidget project, put label on MainWindow and change mainwindow.cpp to

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);
  std::cout << strtod("3.5", 0) << std::endl;
}

MainWindow::~MainWindow() { delete ui; }

There are two new lines

#include <iostream>

and

  std::cout << strtod("3.5", 0) << std::endl;

Everything else is untouched.

Build release and run. The result on console will be 3 which is incorrect. The correct value is 3.5. Why?

In the Debug build it works fine. Simmilar non-Qt project works fine.

I found solution.

The constructor of QApplication class changes localization. It executes the line

setlocale(LC_ALL, "");

Then decimal character is not point longer. One of solutions is using Qt's conversion functions, for example QString::toDouble().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM