Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem while converting QString value to Double gives me a Double Value with wrong Precision.
QtWS25 Last Chance

Problem while converting QString value to Double gives me a Double Value with wrong Precision.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 5.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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.7

    Required output is :
    Line Edit Value is : "12345.67"
    Double Value is : 12345.67

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      Just try

      @bool ok = false;
      double val = ui->lineEdit->text().toDouble(&ok);
      qDebug() << QString("%1").arg(val,0,'g',13);@

      "reference":http://stackoverflow.com/questions/10299996/qstringtodouble-giving-me-double-with-wrong-precision

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Code_ReaQtor
        wrote on last edited by
        #3

        I think the problem is not with QString::toDouble() but with qDebug(). Most of the time it doesn't output precise values.

        Please visit my open-source projects at https://github.com/Code-ReaQtor.

        BuleronB 1 Reply Last reply
        0
        • C Code_ReaQtor

          I think the problem is not with QString::toDouble() but with qDebug(). Most of the time it doesn't output precise values.

          BuleronB Offline
          BuleronB Offline
          Buleron
          wrote on last edited by
          #4

          @Code_ReaQtor
          how can we solve this

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved