Problem while converting QString value to Double gives me a Double Value with wrong Precision.
-
Hi...
I Have a Line Edit,. in that I'm entering a Decimal Value, Then I'm converting the values to a double Values. The Problem is while converting to toDouble(), the decimals values gets Rounded off. How to get the same decimal values as a double value without Rounding it off.
Here's my code...
StringToDouble.h
@#ifndef STRINGTODOUBLE_H
#define STRINGTODOUBLE_H#include <QWidget>
class StringToDouble : public QWidget
{
Q_OBJECTpublic:
explicit StringToDouble(QWidget *parent = 0);
~StringToDouble();private:
Ui::StringToDouble *ui;protected:
bool eventFilter(QObject *target, QEvent *event);
};#endif // STRINGTODOUBLE_H@
StringToDouble.cpp
@
#include "StringToDouble.h"
#include "ui_StringToDouble.h"
#include <QDebug>
#include <QKeyEvent>StringToDouble::StringToDouble(QWidget *parent) :
QWidget(parent),
ui(new Ui::StringToDouble)
{
ui->setupUi(this);
ui->lineEdit->installEventFilter(this);
}StringToDouble::~StringToDouble()
{
delete ui;
}bool StringToDouble::eventFilter(QObject *target, QEvent *event)
{
if(target = ui->lineEdit)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if(keyEvent->key() == Qt::Key_Enter || keyEvent->key()==Qt::Key_Return)
{
QString lineEditValue = ui->lineEdit->text();
qDebug() << " Line Edit Value is : " << lineEditValue;
double value = lineEditValue.toDouble();
qDebug() << " Value is : " << value;
ui->lineEdit->setText(lineEditValue);} } return false;
}@
Enter 12345.67 in the Line Edit and press Enter key or Return Key.
Output of this Coding is:
Line Edit Value is : "12345.67"
Double Value is : 12345.7Required output is :
Line Edit Value is : "12345.67"
Double Value is : 12345.67 -
I think the problem is not with QString::toDouble() but with qDebug(). Most of the time it doesn't output precise values.
-
I think the problem is not with QString::toDouble() but with qDebug(). Most of the time it doesn't output precise values.
@Code_ReaQtor
how can we solve this