简体   繁体   English

在 Qt 项目中使用时 g++ std::strtod 函数中的错误?

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

I use 64-bit ubuntu 18.04 upgraded from 16.04.我使用从 16.04 升级的 64 位 ubuntu 18.04。

Make QWidget project, put label on MainWindow and change mainwindow.cpp to制作 QWidget 项目,在 MainWindow 上放置标签并将 mainwindow.cpp 更改为

#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.控制台上的结果将是 3,这是不正确的。 The correct value is 3.5.正确的值为 3.5。 Why?为什么?

In the Debug build it works fine.在调试版本中它工作正常。 Simmilar non-Qt project works fine.类似的非 Qt 项目工作正常。

I found solution.我找到了解决方案。

The constructor of QApplication class changes localization. QApplication 类的构造函数更改本地化。 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().一种解决方案是使用 Qt 的转换函数,例如 QString::toDouble()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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